用C语言编程:编写一个函数,将两个字符串连接起来

如题所述

1、新建一个工程和.c文件,输入头文件和主函数。

2、定义变量类型。

3、调用cpy函数。

4、定义一个函数,并定义变量类型。

5、用一个For 语句和if语句判断是否为元音。

6、最后加一个字符串结束符,并在主函数中输出。

7、编译。运行。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-06-11
#include<stdio.h>
void main()
{
void con(char sting1[],char sting2[],char sting3[]);
char s1[20],s2[20],s3[40];

printf("Input sting1: ");
scanf("%s",s1);
printf("Input sting2: ");
scanf("%s",s2);
con(s1,s2,s3);
printf("%s\n",s3);
}
void con(char sting1[],char sting2[],char sting3[])
{
int i,j;
for(i=0;sting1[i]!='\0';i++)//////////////////////////多个分号
sting3[i]=sting1[i];

for(j=0;sting2[j]!='\0';j++)//////////////////////////多个分号
sting3[i+j]=sting2[j];
sting3[i+j]='\0';
}
第2个回答  2011-06-18
#include <stdio.h>
void main()
{
char s1[80],s2[40];
int i=0,j=0;
printf("\nInput the first string:");
scanf("%s",s1);
printf("\nInput the second string:");
scanf("%s",s2);
while (s1[i] !='\0')
/**********FOUND**********/
i++;
while (s2[j] !='\0')
/**********FOUND**********/
s1[i++]=s2[j++]; /* 拼接字符到s1 */
s1[i] ='\0';
printf("\nNew string: %s",s1);
}
第3个回答  推荐于2018-02-27
#include<stdio.h>
char *catstr(char *a,char *b)
{
char *p=a,*q=b;
while(*p) p++;
while(*q)*p++=*q++;
*p='\0';
return a;
}
void main()
{
char s1[50]="abcdef";
char s2[]="12345678890";
printf("s1+s2=%s\n",catstr(s1,s2));
}本回答被提问者和网友采纳
第4个回答  2011-06-10
其实楼上的
char *q这个变量没什么意义!
b作为参数传递进来以后本身就另外一个临时的指针变量来保存,并不会影响原先参数的地址值。
不信大可把q变量去掉 (*q)改为(*b) *q++改为*b++结果一样。
相似回答