TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Program to Implement string.Contains() Functionality
Raghu Gurumurthy
May 23
2016
Code
844
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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#