C语言中用s%两个字符串变量在一起如何赋值

就是两个字符串变量在一起是怎样赋值 “s% s%”,貌似不对
2025-03-03 10:22:07
推荐回答(2个)
回答1:

其实用c也很简单的,c里有用于处理字符串的头文件string.h
strcat()函数就是将两个字符串连接
不过在c里面是没字符串变量这个概念的,用字符指针来实现
下面是程序
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
main(){
char
*send
=
"whatyouwant";/*用你想要的东西代替whatyouwant稍改一下可以自己输入,自己完成这个功能吧*/
char
*addr;
addr
=
strcat(send,"@163.com");/*将@163.com连接到send的后面*/
printf("%s",addr);
/*打印结果*/
getch();
}
本人亲自编译通过

回答2:

//使用 sprintf 函数
    char* str1 = "Hello";
    char*str2 = "World";
    char str3[20];
    sprintf(str3,"%s%s",str1,str2);

//或者使用逐个添加的方法。
#include 
int main()
{
    char s1[80],s2[40];
    int i=0,j=0;

    printf("\n请输入第一组字符串(最长40个字符):");
    scanf("%s",s1);
    printf("\n请输入第二组字符串(最长40个字符):");
    scanf("%s",s2);

    while (s1[i] !='\0')
    {
           i++;
    }
    while (s2[j] !='\0')
    {
           s1[i++]=s2[j++];           /* 拼接字符到s1  */
    }
    s1[i] ='\0';

    printf("\n新的字符串: %s",s1);
}