要求
分析:
代码:
自己实现strcmp
int mystrcmp(char* pa, char* pb) {
int sz = sizeof(pa) / sizeof(char);
int i = 0;
for (i = 0; i < sz;i++) {
if (pa[i] < pb[i]) {
return-1;
}
if (pa[i] > pb[i]) {
return 1;
}
}
return 0;
}
int main() {
char s1[100] = { 0 };
char s2[100] = { 0 };
cin.getline(s1, sizeof(s1));
cin.getline(s2, sizeof(s2));
int ret = mystrcmp(s1, s2);
cout << ret << endl;
return 0;
}
还有使用strmcp的方法
int mystrcmp(char *pa,char*pb) {
while (*pa != '\0') {
int ret = strcmp(pa, pb);
if (ret < 0) {
return -1;
break;
}
else if(ret > 0) {
return 1;
break;
}
else {
return 0;
break;
}
pa++;
pb++;
}
}
int main() {
char s1[100] = { 0 };
char s2[100] = { 0 };
cin.getline(s1, sizeof(s1));
cin.getline(s2, sizeof(s2));
int sz = sizeof(s1) / sizeof(s1[0]);
int ret = mystrcmp(s1, s2);
cout << ret << endl;
return 0;
}