Count Sub String From Given String – C Program
Write a C Program to find how many times sub string from given main string.
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,count=0;
char firststr[100],substr[10];
printf("Please, Enter the main string = ");
gets(firststr);
printf("\nPlease, Enter the sub string = ");
gets(substr);
for(i=0; firststr[i]!='\0'; i++)
{
if(firststr[i]==substr[0])
{
for(j=0; firststr[i]==substr[j] && substr[j]!='\0'; i++,j++);
if(substr[j]=='\0')
count++;
}
}
printf("The String count = %d",count);
return 0;
}
The find sub string c program output is:
Please, Enter the main string = This is a my pen.
Please, Enter the sub string = is
The String count = 2