Tuesday 15 February 2011

finding the same words in two strings in c -



finding the same words in two strings in c -

#include <stdio.h> #include <string.h> int main(int argc, char* argv[]) { char* firststring = argv[1]; char* secondstring = argv[2]; int i, j; int flag = 0; (i=0; i<strlen(firststring); i++) { (j=0; j<strlen(secondstring); j++) { if (firststring[i] == secondstring[j]) { flag = 1; } else { flag = 0; break; } } } if (flag == 1) { printf ("true\n"); homecoming 0; } else { printf ("false\n"); } homecoming 1; }

so programme need write compare 2 text strings, , if same words in sec string appears in first string, print true, otherwise, print false.

so example, "i hate you" "hate" --- print true "i hate you" "i walrus" --- print false

but reason, code keeps print false , dunno what's wrong it. able help me?

thanks!

you can seek :

int main(int argc, char* argv[]) { char* firststring = argv[1]; char* secondstring = argv[2]; int flag = 0; int count=0; int len2=strlen(secondstring); while(*firststring!=null) { while((*firststring==*secondstring)&&(*secondstring!='\0')){ firststring++; secondstring++; count++; } firststring++; if(count==len2){ flag=1; break; } else flag=0; } if (flag == 1) { printf ("true\n"); } else { printf ("false\n"); } homecoming 0; }

c string compare words

No comments:

Post a Comment