Substring is present in a string or not
C program to check whether a substring is present in a given string
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char str[MAX], sub_str[MAX];
int i, j, flag;
printf("Enter the string\t:");
gets(str);
printf("Enter substring to be searched\t:");
gets(sub_str);
i = 0;
while(str[i] != '\0')
{
if(str[i] == sub_str[0])
{
j = 0;
flag = 1;
while(sub_str[j] != '\0')
{
if(str[i+j] != sub_str[j])
{
flag = 0;
break;
}
j++;
}
}
if(flag == 1)
{
break;
}
i++;
}
if(flag == 1)
{
printf("\nSubstring found");
}
else
{
printf("\nSubstring not found");
}
getch();
}
Output
Enter the string :welcome to coursecrux.com
Enter substring to be searched :co
Substring found