c语言编程题:定义函数con()将两个字符串连接,主函数输入两个字符串,调用con()连接字符串

2024-11-20 20:07:25
推荐回答(4个)
回答1:

#include

void con(char*s1,char*s2)

{int i,j;

 for(i=0;s1[i];i++);

 for(j=0;s1[i++]=s2[j++];);  

}

int main()

  char s1[200],s2[100];

  gets(s1);

  gets(s2);

  con(s1,s2);

  puts(s1);

  return 0;

}

回答2:

#include
#include

char* con(char *des, const char *src)
{
assert(des != NULL && src != NULL); //断言两个字符串不为空
char *temp = des; //指针temp指向des字符串内存地址
while (*temp != '\0') //如果temp指针没有遇见'\0'
temp++; //temp++
while ((*temp++ = *src++) != '\0'); //实现循环拷贝,当src为'\0'时结束

return des; //返回des值
}

int main()
{
char str1[256] = {0};
char str2[256] = {0};
scanf("%s", str1);
scanf("%s", str2);
char* str3 = con(str1, str2);
printf("str3 =%s\n", str3);

return 0;
}
//示例运行结果
F:\c_work>a.exe
abcd
efgh
str3 =abcdefgh
F:\c_work>a.exe
1234
5678
str3 =12345678

回答3:

回答4:


void concatenate(char string1[],char string2[],char string[]){
int i=0,j=0;
while(string1[i]){
string[i]=string1[i];
i++;
}
while(string2[j]){
string[i]=string2[j];
i++;j++;
}
string[i]='\0';
}