-3

Good evening. So I have a text file which contains multiple lines of text with separators. I need to find the longest text fragment with a condition, that the word's last letter has to be the first letter of the later word. That text fragment can continue for many lines, not only in one line.

F.e.

We have this string array :

Hello, obvious smile eager ruler.
Rave, eyes, random.

So from these two lines we get that our text fragment will be :

Hello, obvious smile eager ruler.
Rave, eyes

Our text fragment ends at word "eyes" because "random" doesn't start with "s".

Next two lines from our txt file :

Johnny, you use.
Eye eager sun.

So from these two other lines we get that our text fragment will be :

Johnny, you use.
Eye eager

Our text fragment ends at word "eager" because "sun" doesn't start with "r".

So we have multiple lines of text with separators in our input file(txt) and I need to find the biggest text fragment through all that text. That text fragment contains words and separators.

I don't even know where to begin, I guess I'll have to use functions like String.Length, Substring and String.Split, maybe Redex might come handy in there, but I'm not really familiar with Redex and it's functions yet.

I tried to explain it as clearly as I can, English isn't my native language, so it's kinda difficult.

My question is : What kind of an algorythm should I use to break my text into separate strings, where one string contains a word and the separator after that word?

feco
  • 53
  • 1
  • 2
  • 9
  • Do you know anything about these separators? And would you want, for example, the ending period? – Iluvatar Dec 05 '16 at 19:46
  • Seperators are . , and empty space I need to know at which line and at which part of that line that text fragment begins (the first letter of the first word in our text fragment) and at which line and which part of that line it ends (the last letter of our last word in our text fragment) – feco Dec 05 '16 at 19:47
  • I would consider breaking it up into sequences of words, finding subsequences that are valid, then evaluating the length of those subsequences and look for the longest one. – Iluvatar Dec 05 '16 at 19:50
  • I know how to split those all lines of text to string array, which contains only the words, without the separators, but I don't know how to split it so that it would contain the word and the separator, that goes right after that word. – feco Dec 05 '16 at 19:52
  • 1
    All that has been posted is a program description. However, we need you to [ask a question](//stackoverflow.com/help/how-to-ask). We can't be sure what you want from us. Please [edit] your post to include a valid question that we can answer. Reminder: make sure you know [what is on-topic here](//stackoverflow.com/help/on-topic), asking us to write the program for you and suggestions are off-topic. – rene Dec 05 '16 at 19:53
  • Sorry, I edited the post now. – feco Dec 05 '16 at 20:04

2 Answers2

0

Lets create the algorithm:

  1. Read file line by line using while loop.
  2. split line by using split method and comma as separator.
  3. use for loop through the created array to compare last character of arr[i] with first character of arr[i+1].
  4. after comparison finished count length of words
  5. compare current length with previous length and save the longest one and text fragment of longer one.
  6. go to step 2 to repeat this work.
  7. print text fragment
Almansour
  • 1
  • 1
  • I think that the correct answer would be `Go Ask your Instructor and tell them that you don't understand the coding assignment` vs expecting someone here to do the work for them.. – MethodMan Dec 05 '16 at 20:21
  • Thanks for the help! You guys helped me to understand the whole process of using/splitting the strings and comparing them – feco Dec 05 '16 at 21:15
0

You need to do the following:

  • Split the text into individual words
  • Compare the first and last letters of the words to see if they are the same
  • If they are not the same, go back to the original text and take the initial text fragment before you encountered that word.

One way to do this would be the following:

String text = "Johnny, you use.\nEye eager sun.";

// Splits the text into individual words
String[] words = text.ToLower().Split(new String[] {" ", ",", ".", "\n"}, StringSplitOptions.RemoveEmptyEntries);

String lastLetter = text.ToLower()[0].ToString();
String newText = text;

// Checks to see if the last letter if the previous word matches with the first letter of the next word
foreach (String word in words)
{
    if (word.StartsWith(lastLetter))
    {
        lastLetter = word[word.Length - 1].ToString();
    }
    else
    {
        newText = text.Split(new String[] { word }, StringSplitOptions.RemoveEmptyEntries)[0]; // Split the original text at the location where the inconsistency happens and take the first text fragment.
        break;
    }
}

Console.WriteLine(text);
Console.WriteLine(newText);
Gevo12321
  • 549
  • 1
  • 5
  • 19
  • I've got it all working, added some more modifications and it's perfectly working, thanks alot for your huge help! – feco Dec 05 '16 at 21:14
  • @Gevo12321 it would be way better if you'll user `.replaceAll(Pattern.compile("[^A-Za-z0-9]").pattern(), " ")` before splitting. Then you can just split by space. – Yashpal Jul 28 '20 at 13:01