Advertisement
Guest User

Untitled

a guest
Jan 19th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. //Write a program that extracts from a given text all sentences containing given word.
  2. //      Example: The word is "in". The text is:
  3. //            We are living in a yellow submarine. We don't have anything else. Inside the submarine is very tight.
  4. //            So we are drinking all the day. We will move out of it in 5 days.
  5. //
  6. //      The expected result is:
  7. //              We are living in a yellow submarine.
  8. //              We will move out of it in 5 days.
  9. //
  10. //      Consider that the sentences are separated by "." and the words – by non-letter symbols.
  11.  
  12. using System;
  13.  
  14. class ExtractSentencesWithGivenWord
  15. {
  16.     static void Main()
  17.     {
  18.         string sText = "We are living in, a yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days.";
  19.         string sSearchWord = "in";
  20.         string[] sSentences = sText.Split(new char[] { '.', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
  21.         for (int i = 0; i < sSentences.Length; i++)
  22.         {
  23.             string[] sWords = sSentences[i].Split(new char[]{'.', ' ', ',', '!', '?'},StringSplitOptions.RemoveEmptyEntries);
  24.             for (int j = 0; j < sWords.Length; j++)
  25.             {
  26.                 if (string.Equals(sWords[j], sSearchWord))
  27.                 {
  28.                     Console.WriteLine((sSentences[i] + ".").Trim());
  29.                 }
  30.             }
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement