Sunday, July 12, 2009

I tried this but it isn't work i want to know the error or how to check the assign of the two strings with c

char a[50];


char b[50];


if(!strcmp(a,b)){


//code


}

I tried this but it isn't work i want to know the error or how to check the assign of the two strings with c
Try





if(strcmp(a,b) == 0)


{


//strings match


}





or





if(strcmp(a,b) != 0)


{


//strings don't match


}





See


http://www.eskimo.com/~scs/cclass/notes/...





Key phrase: "Notice that strcmp does not return a Boolean, true/false, zero/nonzero answer, so it's not a good idea to write something like %26lt;your example%26gt;"
Reply:/* strcmp example */


#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;





int main ()


{


char szKey[] = "apple";


char szInput[80];


do {


printf ("Guess my favourite fruit? ");


gets (szInput);


} while (strcmp (szKey,szInput) != 0);


puts ("Correct answer!");


return 0;


}





Output:





Guess my favourite fruit? orange


Guess my favourite fruit? apple


Correct answer!

blazing star

No comments:

Post a Comment