比较字符串大小。要用字符串比较函数:
strcmp(字符串1,字符串2) // 用这个要加上字符串的头文件 #include<string.h>
strcmp(字符串1,字符串2) // 用这个要加上字符串的头文件 #include<string.h>
root@~ #cat 1.c #include <stdio.h> #include <stdbool.h> int main (void) { bool equalstring (char str[],char s[]); char str[10]; printf ("Input your name,something amazing will happen:"); gets(str); if(equalstring(str,"wuwei")==1) { printf ("cool\n"); } else if(equalstring(str,"pan")==1) { printf ("fool\n"); } else printf ("haha,great fool\n"); return 0; } bool equalstring (char str[],char s[]) { int i=0; bool isequal; while(str[i]==s[i] && str[i]!='\0' && s[i]!='\0') i++; if(str[i]=='\0' && s[i]=='\0') isequal=true; else isequal=false; return isequal; }
// if(equalstring(str,"wuwei")==1) { printf ("cool\n"); } if(equalstring(str,"wuwei")) { printf ("cool\n"); } //else if(equalstring(str,"pan")==1) { printf ("fool\n"); } else if(equalstring(str,"pan")) { printf ("fool\n"); }