Please enable Javascript for better experience...
Write code to find a word in a given string in c#
By Rahul Kumar Jha | Jul 13, 2021 | In Articles | Total Views [ 460 ]
Taged In
(0 Like)

Code to find the text in a given string. This is a word problem asked in many interviews.


static void WordContainsInString()
int f = 0;
Console.WriteLine("Input the string");
string str = Console.ReadLine();
Console.WriteLine("Input the word to search");
string word = Console.ReadLine();
int l = str.Length;
int c = word.Length;
for (int i = 0; i < l; i++)
if (str[i] == word[0])
for (int K = i + 1, j = 1; j < c; j++, K++)
if (str[K] == word[j])
else { f = 0; }
if (f == c - 1)
Console.WriteLine("not found");

Call this method in main method.

static void Main(string[] args)

Hope this helps you.

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer

Public profile: user/profile/99900001

Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments

Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID