Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a program that extracts from a given text all sentences containing given word.
- // Example: The word is "in". The text is:
- // 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.
- //
- // The expected result is:
- // We are living in a yellow submarine.
- // We will move out of it in 5 days.
- //
- // Consider that the sentences are separated by "." and the words – by non-letter symbols.
- using System;
- class ExtractSentencesWithGivenWord
- {
- static void Main()
- {
- 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.";
- string sSearchWord = "in";
- string[] sSentences = sText.Split(new char[] { '.', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < sSentences.Length; i++)
- {
- string[] sWords = sSentences[i].Split(new char[]{'.', ' ', ',', '!', '?'},StringSplitOptions.RemoveEmptyEntries);
- for (int j = 0; j < sWords.Length; j++)
- {
- if (string.Equals(sWords[j], sSearchWord))
- {
- Console.WriteLine((sSentences[i] + ".").Trim());
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement