题解 | #比较字符串大小#
比较字符串大小
https://www.nowcoder.com/practice/963e455fdf7c4a4a997160abedc1951b
#include <iostream>
using namespace std;
int mystrcmp(const char* src, const char* dst);
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;
}
int mystrcmp(const char* src, const char* dst) {
// write your code here......
// 向后比较遍历
int i = 0;
while(src[i] && dst[i] && src[i] == dst[i])
{
++i;
}
// 对应位置的字符的ascii值不相同
if(src[i] > dst[i])
return 1;
else if(src[i] < dst[i])
return -1;
else
return 0;
}

腾讯公司福利 1143人发布