#include<stdio.h>
void
main()
{
int i,j,t,count=0,flag;
char st[100], s[100];
printf("enter a sentence");
gets(st);
printf("\nenter the string whose
occurrence we need to find");
gets(s);
for(i=0;st[i] != '\0';i++)
{
flag=0;
for(j=0,t=i;s[j] != '\0'; j++,t++)
{
if(st[t] != s[j])
{
flag=1;
break;
}
}
if(flag == 0)
count++;
}
printf("number
of occurrences = %d", count);
}
(OR)
/*WITH OPTIMIZATION*/
#include<stdio.h>
#include
<string.h>
void
main()
{
int i,j,t,count=0,flag,len;
char st[100], s[100];
printf("enter a sentence");
gets(st);
printf("\nenter the string whose
occurrence we need to find");
gets(s);
len=strlen(s);
for(i=0;st[i+len-1] != '\0';i++)
{
flag=0;
for(j=0,t=i;s[j] != '\0'; j++,t++)
{
if(st[t] != s[j])
{
flag=1;
break;
}
}
if(flag == 0)
{
count++;
i=i+len-1;
}
}
printf("number
of occurrences = %d", count);
}
(OR)
/* USING STRTOK() FUNCTION*/
#include <string.h>
#include <string.h>
#include
<stdio.h>
void main ()
{
char str[80] = "KLU is a great
university. Iam a KLU student";
char s[2] = " ";
char *token;
int count=0;
token = strtok(str, s);
while( token != NULL )
{
if(strcmp(token,"KLU")==
0)
count++;
token = strtok(NULL, s);
}
printf("Number of
occurrences=%d",count);
}
No comments:
Post a Comment