Oct 3, 2011

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

C Strings:
Write a C program to accept a string and a substring and check if the substring is present in the given string. Program search's given substring is in the string or not, and prints the appropriate message. Read more about C Programming Language .
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                         Happy Coding
***********************************************************/




#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 substring:");
 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();
}
Read more Similar C Programs
Searching in C

C Strings
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!