原型strcmp(const char *s1,const char * s2);
用法:#include <string.h>
功能:比较字符串s1和s2。
说明:
当s1=s2时,返回值=0
当s1>s2时,返回值>0
"A"<"B" "a">"A" "computer">"compare"
特别注意:strcmp(const char *s1,const char * s2)这里面只能比较字符串,不能比较数字等其他形式的参数。
- #include<stdio.h>
- int strcmp(char const *str1,char const *str2);
- int main()
- {
- int n = 0;
- char a[128] = {0};
- char b[128] = {0};
- gets(a);
- gets(b);
- n = strcmp(a,b);
- printf("n = %d\n",n);
- return 0;
- }
- int strcmp(char const *str1,char const *str2)
- {
- char *p;
- char *q;
- p = (char *)str1;
- q = (char *)str2;
- int n = 0;
- while((*str1) && (*str1 == *str2))
- {
- str1++;
- str2++;
- }
- if( *str1 > *str2 )
- {
- n = *str1 - *str2;
- return n;
- }
- else if( *str1 < *str2 )
- {
- n = *str1 - *str2;
- return n;
- }
- else
- {
- return n;
- }
- }