Raghu Gurumurthy
Write a C# program searching the first occurrence of matching string in the given string, We may say that it behaves as “string.Contains()” built in function.
By Raghu Gurumurthy in .NET on May 23 2016
  • Raghu Gurumurthy
    May, 2016 23

    namespace StringMatching {class Program{static void Main(string[] args){Console.WriteLine("Enter the string");string s1 = Convert.ToString(Console.ReadLine()).Trim().ToUpper();Console.WriteLine("Enter the string to be search");string s2 = Convert.ToString(Console.ReadLine()).Trim().ToUpper();int sourceIndex = 0;int searcIndex = 0;bool searchResult = false;int matchingCount = 0;while (sourceIndex < s1.Length){if (s2[searcIndex].Equals(s1[sourceIndex])){searcIndex++;sourceIndex++;searchResult = true;matchingCount++;if(matchingCount == s2.Length){break;}}else{searcIndex=0;sourceIndex++;searchResult = false;}if (sourceIndex == s1.Length) searchResult = false;}if(searchResult){Console.WriteLine("Searching string is found");}else{Console.WriteLine("Searching string is not found");}Console.ReadLine();}} }

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS