//2.实现strcmp
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2){
int i;
for (i = 0; str1[i] != '\0' && str2[i] != '\0'; ++i){
if (str1[i] < str2[i]){
return -1;
}
else if (str1[i] > str2[i]){
return 1;
}
else{
return 0;
}
}
}
int main(){
char a[1024] = "abc";
char b[1024] = "ABC";
int ret = my_strcmp(a,b);
if (ret == 1){
printf("a[] > b[]\n");
}
else if (ret == -1){
printf("a[] < b[]\n");
}
else if (ret == 0){
printf("a[] = b[]\n");
}
else{
;
}
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<assert.h> //断言
int my_strcmp(const char* dst,const char* src){
assert(dst != NULL);
assert(src != NULL);
int i;
while ((i = *src - *dst) == 0 && *dst++ && src++);
return i;
}
int main(){
char ch1[20] = { "abc" };
char ch2[20] = { "abcd" };
int k = 0;
k = my_strcmp(ch1,ch2);
if (k > 0){
printf("ch1 > ch2");
}
else if (k < 0){
printf("ch1 < ch2");
}
else if (k == 0){
printf("ch1 = ch2");
}
system("pause");
return 0;
}