Live Webinar: Prompt Engineering: Skill Everyone Must Learn Today
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Program to Implement string.Contains() Functionality
WhatsApp
Raghu Gurumurthy
May 23
2016
913
0
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();
}
}
}
C#
Up Next
Program to Implement string.Contains() Functionality