题目:
不调用库函数,自己实现字符串的比较操作:该操作当比较的两个字符是都是字母,且两个字符互为大小写(如a和A、e和E)时认为两个字符相同,否则不同,其比较结果按这两个字符的原值确定。函数的返回值规定如下:
返回值 < 0:第一个不匹配的字符在 ptr1 中的值低于 ptr2 中的值
返回值 == 0:两个字符串的内容相等
返回值 > 0:第一个不匹配的字符在 ptr1 中的值大于在 ptr2 中的值
int str_compare(const char* ptr1, const char* ptr2);
代码:
#include "dsstring.h" //请不要删除,否则检查不通过
#include <stdio.h>
#include <stdlib.h>
int str_compare(const char* ptr1, const char* ptr2)
{
char a, b;
int i, n, k;
i = 0;
n = 0;
k = 0;
while (ptr1[n] != '\0') {
n++;
}
while (ptr2[k] != '\0') {
k++;
}
while (i <= n && i <= k) {
a = ptr1[i];
b = ptr2[i];
if (a == b || (a - 32) == b || (a + 32) == b) {
i++;
} else if (a < b) {
return -1;
} else {
return 1;
}
}
if (n == k) {
return 0;
} else if (n < k) {
return -1;
} else {
return 1;
}
}
代码仅供参考,望多多思考