字符串链接比较源代码
作者:互联网
#include <stdio.h>
int my_strlen(char *p) //计算字符串的长度
{
int len = 0;
while(*p != '\0')
{
len++;
p++;
}
return len;
}
int my_strcmp(char *ptr1,char *ptr2) //比较两个字符串是否相等
{
if(my_strlen(ptr1) != my_strlen(ptr2) )
{
printf(“两个字符串长度不等!\n”);
return -1;
}
while( *ptr1 != '\0' )
{
if( *ptr1 != *ptr2 )
{
return -1;
}
ptr1++;
ptr2++;
}
return 1;
}
void my_strcat(char *str1,char *str2) //连接两个字符串
{
while(*str1 != '\0')
{
str1++;
} //将str1遍历到最后,记录地址
while(*str2 != '\0')
{
*str1 = *str2;
str1++;
str2++;
}
*str1 = '\0';
}
int main()
{
char src1[100];
char src2[100];
int flag = 0; //用flag记录返回值
printf("please input src1:\n");
scanf("%s",src1);
//fgets(src1,100,stdin);
printf("please input src2:\n");
scanf("%s",src2);
//fgets(src2,100,stdin);
flag = my_strcmp(src1,src2);
if(flag == 1)
{
printf("YES!\n");
}
else
{
printf("NO!\n");
}
my_strcat(src1,src2); //输出连接后的字符
printf("the result is:%s\n",src1);
return 0;
}
标签:++,str1,char,src1,printf,字符串,源代码,my,链接 来源: https://blog.csdn.net/qq_51450595/article/details/116561498