prev


Character Arrays - String manipulations:

97.grep command count option - whether given pattern/string exist or not (16.05.2010)

/*
Program# 97
date : 10-01-2009
No.of Times Pattern exists --will work only if input given as i love this universe$universe$ In : i love this universe$ universe$ bug:because '\n' gets into b[0]
*/

#include <stdio.h>
main(){
char a[100],b[100];
int i,j,k,w=0;

i=0;
scanf("%c",&a[i]);
while(a[i]!='$'){
i++;
scanf("%c",&a[i]);
}
a[i]='\0';

j=0;
scanf("%c",&b[j]);
while(b[j]!='$'){
j++;
scanf("%c",&b[j]);
}
b[j]='\0';

i=0;
while(a[i]!='\0'){
j=0;
k=i;

while((b[j]==a[k]) && (b[j]!='\0')){
j++;
k++;
}

if(b[j]=='\0'){
w++;
}
i++;

}
if(w==0)
printf("\n pattern not exists");
else
printf("\n Pattern exists %d times",w);
}



next
blog comments powered by Disqus