#include <stdio.h>
void mystrcpy(char *d , char *s )
{
while(*s) *(d++) = *(s++);
*d = '\0';
}
void mystrcat(char *d , char *s )
{
while(*d) d++;
while(*s) *(d++) = *(s++);
*d = '\0';
}
int mystrcmp(char *s , char *t )
{
while( *s && *t && *s == *t ) s++,t++;
return *s - *t;
}
int main(int argc, char *argv[])
{
char s1[32] = "sdfsfsdgsd";
char s2[32] = "sdgsdf";
puts(s1);
puts(s2);
printf("cmp:%d\n",mystrcmp(s1,s2));
mystrcpy(s1,s2);
puts(s1);
puts(s2);
printf("cmp:%d\n",mystrcmp(s1,s2));
mystrcat(s1,s2);
puts(s1);
puts(s2);
printf("cmp:%d\n",mystrcmp(s1,s2));
return 0;
}
温馨提示:内容为网友见解,仅供参考
...通过自定义函数实现字符串处理函数strcat、 strcpy、strcmp...
strcat(char *p1,char *p2,char *p){ while(*p1) \/\/检查指针是否到了'\\0'{ p=*p1; \/\/赋值 p1++; \/\/指针指向下一个字符 p++;} while(*p2){ p=*p2;p2++;p++;} p='\\0'; \/\/为str3结尾加上'\\0'} 这个是strcat是3个形参的,两个形参的是这样写的 void strcat(char *p1, ...
C语言的小程序,求解
mystrcpy(str1,tmpstr);printf("%s\\n",str1);mystrncpy(str1,str2,3);printf("%s\\n",str1);mystrnmcpy(str1,str2,1,3);printf("%s\\n",str1);printf("%d\\n",mystrcmp(str1,str2));return 0;}