#include <bits/stdc++.h>
//自己定义的mystrcmp()函数
int mycmp(const char *s1, const char *s2){
//当出现两个指针所对应的值不一样时或者比较到底了退出
while(*s1 == *s2 && *s1 != '\0'){
//两个指针同时往下挪移一位进行比较
s1++;
s2++;
}
//返回比较的差值
return *s1 - *s2;
}
//const表示是常量 自己事先定义的字符数组就是常量
size_t mylen(const char* s){
int index = 0;
//'\0'是数组的结尾标志
while(s[index] !='\0'){
index++;
}
return index;
}
int main()
{
char len[] = "hello";
//这是算的真实长度5
printf("mylen=%d\n",mylen(len));
//这是算上了'\0'这个单位 长度是6
printf("sizeof=%d\n",sizeof(len));
char s1[] = "abc";
char s2[] = "abC";
printf("差值=%d\n",mycmp(s1,s2));
printf("差值=%d\n",strcmp(s1,s2));
return 0;
}
strlen strcmp函数C
最新推荐文章于 2024-04-16 12:15:28 发布