1
votes

I need to find in which lines of both text files is my longest text fragment. I found it, but I don't know how to find in which lines is my text fragment. Maybe I need to change my ReadFile method somehow?

string[] Fragment(string[] wordsWithSeparatos1, string[] wordsWithSeparators2)
{
    int place = 0;
    string[] fragment = new string[1000];
    for (int i = 0; i < wordsWithSeparatos1.Length; i++)
    {
        for (int j = 0; j < wordsWithSeparators2.Length; j++)
        {
            if (wordsWithSeparatos1[i] == wordsWithSeparators2[j])
            {
                fragment[place] += wordsWithSeparatos1[i] + ' ' ;
                i++;
            }
            else
            {
                if (fragment[place] != null)
                {
                    place++;
                }
            }
        }
    }
    return fragment;
}

void LongestFragment(string[] fragment)
{
    foreach (var item in fragment.OrderByDescending(x => fragment.Length).Take(1))
    {
        Console.WriteLine(item);
    }
}

string[] ReadFile(string fn)
{
    char[] space = { ' ' };
    string lines = File.ReadAllText(fn, Encoding.GetEncoding(1257));
    string[] wordsWithSeparators = new string[1000];
    wordsWithSeparators = lines.Split(space, StringSplitOptions.RemoveEmptyEntries);
    return wordsWithSeparators;
}

Text1.txt: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Text2.txt: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

1
your lines have a \r\n or \n end of line character ?Frenchy
No it doesnt. I will upload text files shortlySam
ok i see you load all text file in string, but if your text has no eol, you have just one line??Frenchy
Now yes. I think i need to use File.ReadAllLines. In first text file i have 4 lines, in second 2Sam
yes you modify the logic of your program by reading and splitting each line , and you add the split result in list<string> (you use File.ReadLine Method). So you could keep the num of line or the content each time the fragment is longerFrenchy

1 Answers

1
votes

an example of solution: (the code is easily to understand) you trap the longest fragment and the line which contains it

       string keeplineoflongestfrag="";
       string stlongestfrag="";
       int longestfrag = 0;

       using (StreamReader file = new StreamReader((filepath, System.Text.Encoding.Default))
        {

            string line;
            char[] space = { ' ' };
            while ((line = file.ReadLine()) != null)
            {
                var wordsWithSeparators = lines.Split(space, StringSplitOptions.RemoveEmptyEntries);
                bool modify = false
                foreach(var lg in wordsWithSeparators)
                {
                    if (lg.Length > longestfrag)
                    {
                        longestfrag = lg.Length;
                        stlongestfrag = lg;
                        if (!modify)
                        {
                            keeplineoflongestfrag= line ;
                            modify = true;
                        }
                    }
                }
            }
        }