字符串比较函数实现方法:
- #include <stdio.h>
- int mystrcmp(const char *src, const char *des)
- {
- while(*src == *des)
- {
- if (*src == '\0')
- return 0;
- src++;
- des++;
- }
- return *src - *des;
- }
- int main(int argc, const char *argv[])
- {
- int flag;
- char string[]="hello world";
- char string1[]="hello world";
- flag = mystrcmp(string, string1);
- if (flag == 0)
- printf("两字符串相等\n");
- else if(flag > 0)
- printf("string大于string1\n");
- else if (flag < 0 )
- printf("string小于string1\n");
- return 0;
- }
字符串拷贝函数实现方法:
- #include<stdio.h>
- void mystrcpy(const char *src, char *des)
- {
- while(*src)
- {
- *(des++) = *(src++);
- }
- *des = '\0';
- return ;
- }
- int main(int argc, const char *argv[])
- {
- char string[] = "hello world";
- char destination[12]={0};
- mystrcpy(string, destination);
- printf("src: %s\n", string);
- printf("destination: %s\n", destination);
- return 0;
- }