C语言问题 编写一程序将两个字符串连起来

大神们,打扰一下,C语言问题 编写一程序将两个字符串连起来
最新回答
宛若晴空

2024-05-02 19:15:22

#include <stdio.h>
#include <string.h>
int main ()
{
    char str1[10],str2[5],*p,i;//这里用str1接收拼接后的字符串,确保str1大小够放!! 注意要留1位保存结束符'\0'
    strcpy(str1,"abcd");strcpy(str2,"efgh");
    printf("原字符串分别为:%s   %s\n\n",str1,str2);

    strcat(str1,str2);
    printf("用strcat拼接后字符串:%s\n\n",str1);

    strcpy(str1,"abcd");strcpy(str2,"efgh");
    p=&str1[strlen(str1)];
    for(i=0;i<strlen(str2);i++)
        *p++=str2[i];
    *p=0;
    printf("不用strcat拼接后字符串:%s",str1);
    return 0;
}
守护在此方

2024-05-02 08:21:05

一、用strcat函数:

#include "stdio.h"
#include "string.h"
int main(int argc,char *argv[]){
char a[100]="abcdefg",b[]="1234567";
printf("%s\n",strcat(a,b));
return 0;
}

二、不用strcat函数:

#include "stdio.h"
int main(int argc,char *argv[]){
char a[100]="abcdefg",b[]="1234567",*pa=a,*pb=b;
while(*pa)
pa++;
while(*pa++=*pb++);
printf("%s\n",a);
return 0;
}

反复看都是今天的提问,提交了变成 2015-12-04的提问了,且已经采纳别人了——百度知道最近怎么了?这么忽悠人!

酒爷

2024-05-02 06:48:06

#include<
stdio.h
>
#include<
string.h
>
int main(void){
    char *ch1;
    char *ch2;
    printf("请输入第一个
字符串
:");
    scanf("%s",ch1);
    printf("请输入第二个字符串:");
    scanf("%s",ch2);
    strcat(ch1,ch2);
    printf("%s",ch1);
    return 0;
}
晚街

2024-05-02 15:16:08

#include<stdio.h>
void f(char *a, char *b)
{

while(*a++);
a--;
while(*a++=*b++);

}
void main()
{
char a[100], b[100];
gets(a);
gets(b);
f(a, b);
puts(a);
}
寒烟雾柳

2024-05-02 15:29:12

你的描述不够详细,我写了个给你参考

1 用strcat函数
#include <string>
#include <iostream>
using namespace std;
int main(void) {
char s1[20]="abcd";
char s2[]="def";
strcat(s1,s2);
cout<<s1<<endl;
return 1;
}
2 不用strcat函数
#include <string>
#include <iostream>
using namespace std;
int main(void) {
char s1[20]="abcd";
char s2[]="def";
char *p;
p=&s1[strlen(s1)];
strcpy(p,s2);
cout<<s1<<endl;
return 1;
}