自定义函数实现strcmp实现查找最大字符串
strcmp: 比较2个字符串s1, s2, 返回一个整数, 小于, 等于, 大于0 代表s1小于, 等于, 大于s2
代码如下:
include <stdlib.h>
#include <stdio.h>
//
//二维字符数组; 求最值
//
int my_strcmp(char *s1, char *s2); //参数是字符串的指针
int main(int argc, const char *argv[])
{
char *parr[] = {"HelloW ","HelloO","HelloR","HelloL", "HelloD" }; //指针数组,本质是数组,用于存放指针
int max=0; //用于标记最大字符串下标
for(int i=1; i<5; i++ ){
if(my_strcmp(parr[i], parr[max]) >0 ){
max = i;
}
}
printf("the max string = %s\n", parr[max]);
return 0;
}
int my_strcmp(char *s1, char *s2)
{
int r; //字符串比较差值, r>0: s1>s2; r=0: s1=s2; r<0: s1<s2
for(; *s1!='\0'; s1++, s2++)
{
r = *s1 - *s2;
//printf("*(s1+%d) = %c, *(s2+%d) = %c\n", i, *(s1+i),i,*(s2+i) );
//printf("r=%d\n", r);
}
return r;
}