Monday 8 December 2014

C program to find how many times str2 found in str1

program to find how many times str2 found in str1
#include<stdio.h>
#include<string.h>
main()
{
char str1[100],str2[50],*ptr;
char ch;
int k=0;
printf("enter a string1\n");
scanf("%[^\n]s",str1);
printf("enter a string2\n");
scanf(" %[^\n]s",str2);
ptr=str1;
while(ptr=strstr(ptr,str2))
{
k++;
ptr++;
}
printf("number of times string is repeated=%d\n",k);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array8.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string1
welcome welcome come coming cometo home
enter a string2
come
number of times string is repeated=4