Skip to content

Instantly share code, notes, and snippets.

@valiyo
Created February 3, 2013 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valiyo/4701195 to your computer and use it in GitHub Desktop.
Save valiyo/4701195 to your computer and use it in GitHub Desktop.
[C#] Домашно Strings and Text Processing - 8 задача Extract all sentences containing given word.
using System;
/* 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. */
class ExtractSentences
{
static void Main()
{
string target = "in";
string input = "We are living in an 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[] sentences = input.Split('.');
foreach (string sentence in sentences)
{
string trimmedSentence = sentence.Trim();
string[] splitedSentence = new string[input.Length];
splitedSentence = trimmedSentence.Split(' ', ',', '.', ';', ':', '!', '?', '(', ')');
foreach (string word in splitedSentence)
{
if (string.Compare(word, target, true) == 0)
{
Console.WriteLine("{0}.", trimmedSentence);
}
}
}
Console.WriteLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment