Advertisement
Assi

13. Strings and Text Processing 8. ShowSentenceWithWordIN

Jan 25th, 2013
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. using System;
  2.  
  3. class ShowSentenceWithWordIN
  4. {
  5.     static void Main()
  6.     {
  7.         string str = @"In A in 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.";
  8.  
  9.         ShowSentenceWithWord(str);
  10.     }
  11.  
  12.     static void ShowSentenceWithWord(string str)
  13.     {
  14.         int startIndex = 0;
  15.         int endIndex = 0;
  16.  
  17.         bool afterInDot = false;
  18.         bool afterInSpace = false;
  19.  
  20.         bool beforeInDot = false;
  21.         bool beforeInSpace = false;
  22.         bool iZeroBased = false;
  23.  
  24.         for (int i = 0; i < str.Length; i++)
  25.         {
  26.             if (str.Substring(i, 1) == "." && startIndex < i)
  27.             {
  28.                 endIndex = i - 1;
  29.  
  30.                 for (int k = startIndex; k <= endIndex; k++)
  31.                 {
  32.                     if (str.Substring(k, 2).ToLower() == "in")
  33.                     {
  34.                         if (str.Substring(k, 2).ToLower() == "in" && k != 0)
  35.                         {
  36.                             if (str.Substring(k + 2, 1) == ".")
  37.                             {
  38.                                 afterInDot = true;
  39.                             }
  40.                             if (str.Substring(k + 2, 1) == " ")
  41.                             {
  42.                                 afterInSpace = true;
  43.                             }
  44.                             if (str.Substring(k - 1, 1) == " ")
  45.                             {
  46.                                 beforeInSpace = true;
  47.                             }
  48.                             if (str.Substring(k - 1, 1) == " ")
  49.                             {
  50.  
  51.                             }
  52.                         }
  53.  
  54.                         if (str.Substring(k, 2).ToLower() == "in" && k == 0)
  55.                         {
  56.                             iZeroBased = true;
  57.                             if (str.Substring(k + 2, 1) == " ")
  58.                             {
  59.                                 afterInSpace = true;
  60.                             }
  61.                         }
  62.                     }
  63.  
  64.                     if (beforeInSpace && afterInSpace || beforeInSpace && afterInDot || iZeroBased && afterInDot)
  65.                     {
  66.                         int length = endIndex - startIndex;
  67.                         Console.WriteLine(str.Substring(startIndex, length + 2).Trim());
  68.                         startIndex = 0;
  69.                         endIndex = 0;
  70.  
  71.                         afterInDot = false;
  72.                         afterInSpace = false;
  73.  
  74.                         beforeInSpace = false;
  75.                         iZeroBased = false;
  76.                         break;
  77.                     }
  78.                 }
  79.                 startIndex = i + 1;
  80.             }
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement