下面程序使用strcmp()验证答案是否正确:
char try[10];
puts("Who is 1+1?");
gets(try);
while(strcmp(try,"2")!=0)
{
puts("No,it's wrong.Try again.");
gets(try);
}
puts("OK,That's right");
下面程序说明各种情况下,strcmp()的返回值:
printf("strcmp(\"a\",\"a\")=%d\n",strcmp("a","a"));
printf("strcmp(\"a\",\"b\")=%d\n",strcmp("a","b"));
printf("strcmp(\"b\",\"a\")=%d\n",strcmp("b","a"));
printf("strcmp(\"a\",\"c\")=%d\n",strcmp("a","c"));
printf("strcmp(\"c\",\"a\")=%d\n",strcmp("c","a"));
printf("strcmp(\"abc\",\"abd\")=%d\n",strcmp("abc","abd"));
printf("strcmp(\"abc\",\"abcd\")=%d\n",strcmp("abc","abcd"));
printf("strcmp(\"abc\",\"abbd\")=%d\n",strcmp("abc","abbd"));