编写一个字符串比较函数my_strcmp,若相等输出0,否则输出两个字符串中第一个不相同字符的ASCII码差值。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <string.h>
int my_strcmp(char a[], char b[])
{
int i,x;
char *p1=a,*p2=b;
while (*p1 != '\0' && *p2 != '\0') {
if (*p1 != *p2) {
x = *p1 - *p2;
break;
}
p1++;
p2++;
}
if (*p1 == '\0' || *p2 == '\0') {
x = *p1 - *p2;
}
return x;
}
void main(void)
{
char a[100], b[100];
gets(a);
gets(b);
printf("%d\n", my_strcmp(a, b));
}
将第11行指针改成数组
#include <stdio.h>
#include <math.h>
#include <string.h>
int my_strcmp(char a[], char b[])
{
int i,x;
char *p1=a,*p2=b;
while (*a!= '\0' &&*b != '\0') {
if (*p1 != *p2) {
x = *p1 - *p2;
break;
}
p1++;
p2++;
}
if (*p1 == '\0' || *p2 == '\0') {
x = *p1 - *p2;
}
return x;
}
void main(void)
{
char a[100], b[100];
gets(a);
gets(b);
printf("%d\n", my_strcmp(a, b));
}