1
Reply

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.

Raghu Gurumurthy

Raghu Gurumurthy

May 23, 2016
153
0

    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();}} }

    Raghu Gurumurthy
    May 23, 2016
    0