Linux系统下C语言练习:指针实现mystrcmp、mystrlen
程序员文章站
2022-04-03 17:06:34
Linux系统下C语言练习:指针实现mystrcmp、mystrlen
#include
#include
char *mystrcmp(char *str, cha...
Linux系统下C语言练习:指针实现mystrcmp、mystrlen
#include #include char *mystrcmp(char *str, char *str1) { while(*(str++) - *(str1++) == 0 && *str != '\0' && *str != '\0'); char *p1 = "第一个数小于第二个数"; char *p2 = "第一个数大于第二个数"; char *p3 = "第一个数等于第二个数"; str--; str1--; if((*str - *str1) == 0 && (*(str + 1) =='\0') && (*(str1 + 1) != '\0')) { return p1; } if((*str - *str1) == 0 && (*(str1 + 1) =='\0') && (*(str + 1) != '\0')) { return p2; } if((*str - *str1) == 0 && (*(str1 + 1) =='\0') && (*(str + 1) == '\0')) { return p3; } if(*str - *str1 < 0) { return p1; } if(*str - *str1 > 0) { return p2; } } int mystrlen(char *p) { int count = 0; while(*p++ !='\0') count++; return count; } int main() { printf("请输入两串字符\n"); char *str = (char *)malloc(sizeof(char) * 20); char *str1 = (char *)malloc(sizeof(char) * 20); if(NULL == str || NULL == str1) { printf("error\n"); exit(1); } scanf("%s%s", str, str1); printf("第一个字符串长度为:%d\n", mystrlen(str)); printf("第二个字符串长度为:%d\n", mystrlen(str1)); printf("%s\t%s\n", str,str1); printf("%s\n", mystrcmp(str, str1)); free(str); free(str1); return 0; }