Friday, October 14, 2011

Write a C program to find how many times given substring is exist ?

#include
main()
{
int i=0,j=0,k=0,count=0,l=0,m=0;
char a[100],b[100];
printf("\nEnter the string \t:");
gets(a);
printf("\nEnter sub string \t:");
gets(b);
l=strlen(b);
while (a[i]!=EOF)
{
if (a[i]==b[j])
{
i++;
j++;
m=1;

if (j==l)
{
j=0;
k=1;
count=count+1;
}
}
else
{
if (m==1)
{
j=0;
m=0;
}
else
i++;
}
}

if (k==1)
{
printf("\n\nThe Entered sub string is present in the given string\n\n");
printf("\nIt is present %d times in the given string\n\n",count);
}
else
{
if (k==0)
printf("\n\nThe Entered sub string is not present in the given string\n\n");
}
}

No comments:

Post a Comment