Program to Implement string.Contains() Functionality

  1. namespace StringMatching  
  2. {  
  3.    class Program  
  4.    {  
  5.          static void Main(string[] args)  
  6.          {  
  7.                      Console.WriteLine("Enter the string");  
  8.                      string s1 = Convert.ToString(Console.ReadLine()).Trim().ToUpper();  
  9.                      Console.WriteLine("Enter the string to be search");  
  10.                      string s2 = Convert.ToString(Console.ReadLine()).Trim().ToUpper();  
  11.                      int sourceIndex = 0;  
  12.                      int searcIndex = 0;  
  13.                      bool searchResult = false;  
  14.                      int matchingCount = 0;  
  15.                      while (sourceIndex < s1.Length)  
  16.                      {  
  17.                            if (s2[searcIndex].Equals(s1[sourceIndex]))  
  18.                            {  
  19.                                  searcIndex++;  
  20.                                  sourceIndex++;  
  21.                                  searchResult = true;  
  22.                                  matchingCount++;  
  23.                                  if(matchingCount == s2.Length)  
  24.                                 {  
  25.                                        break;  
  26.                                  }  
  27.                             }  
  28.                           else  
  29.                            {  
  30.                                  searcIndex=0;  
  31.                                  sourceIndex++;  
  32.                                  searchResult = false;  
  33.                            }     
  34.                         if (sourceIndex == s1.Length) searchResult = false;  
  35.                      }  
  36.                      if(searchResult)  
  37.                      {  
  38.                            Console.WriteLine("Searching string is found");  
  39.                      }  
  40.                      else  
  41.                      {  
  42.                            Console.WriteLine("Searching string is not found");  
  43.                      }  
  44.                      Console.ReadLine();  
  45.       }  
  46.    }  
  47. }