C语言字符篇(三)字符串比较函数
#include <string.h> | |
int strcmp(const char *s1, const char *s2);
|
比较字符串s1和s2 |
int strncmp(const char *s1, const char *s2, size_t n); | 比较字符串s1和s2前n个字符 |
如果两个字符一样大,返回值为 0
如果s1>s2,则返回正值,
如果s1<s2,则返回负值.
-------------------------------------------------------
int main(int argc, char **argv) {
const char *s1="hello";
const char *s2="hello";
const char *s3="aello";
int ret1,ret2;
ret1=strncmp(s1,s2,strlen(s1));
ret2=strncmp(s1,s3,strlen(s1));
printf("%d\n",ret1); //0
printf("%d\n",ret2); //7
return 0;
}