28. Find the Index of the First Occurrence in a String
Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Leetcode Qn :
public class Solution {
public int StrStr(string haystack, string needle) {
string mainString = haystack;
string searchString = needle;
int indexOfFirstOccurrence = mainString.IndexOf(searchString);
if (indexOfFirstOccurrence != -1)
{
Console.WriteLine($"The first occurrence of '{searchString}' is at index: {indexOfFirstOccurrence}");
return indexOfFirstOccurrence;
}
else
{
Console.WriteLine($"'{searchString}' not founds in the string.");
}
return indexOfFirstOccurrence;
}
}
Comments
Post a Comment