C program to accept a string and a substring and check if the substring is present in the given string

by Nideesh C on April 12, 2011 · 0 comments

in C Programming




/* C program to accept a string and a sub string and check if the sub string is present in the given string */

#include<stdio.h>
#include<conio.h>

void main()
{
char str[80],search[10];
int count1=0,count2=0,i,j,flag;

clrscr();

puts(“Enter a string:”);
gets(str);

puts(“Enter search sub string:”);
gets(search);

while (str[count1]!=’\0′)
count1++;

while (search[count2]!=’\0′)
count2++;

for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=search[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
puts(“SEARCH SUCCESSFUL!”);
else
puts(“SEARCH UNSUCCESSFUL!”);
getch();
}
/*——————————
Output
Enter a string:
Hello how are you?
Enter search sub string:
how
SEARCH SUCCESSFUL!
——————————*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to read a string and check whether it is a palindrome or not (without using library functions). Output the given string along with suitable message.
  2. C program to read two strings and concatenate them (without using library functions). Output the concatenated string along with the given string
  3. C program to check whether a given number is prime or not and output the given number with suitable message
  4. Sample – Program to enter a string and find its length.
  5. C program to accept N numbers sorted in ascending order and to search for a given number using binary search. Report success or failure in the form of suitable messages

Leave a Comment

Previous post:

Next post: