C语言手动连接字符串怎么弄?
#include<stdio.h>intlength(char*a){intcounter=0;while(*a){counter++;*a++;}returncount...
#include<stdio.h>
int length(char *a)
{
int counter=0;
while(*a)
{
counter++;
*a++;
}
return counter;
}
void lianjie(char *a,char *b)
{
int d=length(a);
char *p;
p=a;
a=(a+d+1);
while(*b);
{
*a=*b;
*a++;
*b++;
}
a=p;
}
int main()
{
char a[40];
char b[40];
printf("put in a string:");
scanf("%s",a);
printf("put in b string:");
scanf("%s",b);
lianjie(a,b);
printf("string a now is:%s\n",a);
return 0;
} 展开
int length(char *a)
{
int counter=0;
while(*a)
{
counter++;
*a++;
}
return counter;
}
void lianjie(char *a,char *b)
{
int d=length(a);
char *p;
p=a;
a=(a+d+1);
while(*b);
{
*a=*b;
*a++;
*b++;
}
a=p;
}
int main()
{
char a[40];
char b[40];
printf("put in a string:");
scanf("%s",a);
printf("put in b string:");
scanf("%s",b);
lianjie(a,b);
printf("string a now is:%s\n",a);
return 0;
} 展开
2个回答
展开全部
程序中的错误,详见注释,希望能帮到您!修改后代码如下:
#include <stdio.h>
int length(char *a)
{
int counter=0;
while(*a)
{
counter++;
*a++;
}
return counter;
}
void lianjie(char *a,char *b)
{
int d=length(a);
char *p;
p=a;
//a=(a+d+1);
a=a+d;
//while(*b);
while(*b)
{
*a=*b;
*a++;
*b++;
}
*a='\0';
a=p;
}
int main()
{
char a[40];
char b[40];
printf("put in a string:");
scanf("%s",a);
printf("put in b string:");
scanf("%s",b);
lianjie(a,b);
printf("string a now is:%s\n",a);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询