C++ 在C+中连接字符数组+;

C++ 在C+中连接字符数组+;,c++,arrays,char,concatenation,C++,Arrays,Char,Concatenation,我有以下代码,并希望以一个字符结束,例如:“你好,你好吗?”(这只是我试图实现的一个示例) ,使用 STD::String ,以及 Trase+< /C> >,专门设计来解决这样的问题。 #include <iostream> #include <string> using namespace std; int main() { string foo( "hello" ); string test( "how are" ); cout <&

我有以下代码,并希望以一个字符结束,例如:“你好,你好吗?”(这只是我试图实现的一个示例)

<如何连接2个字符数组加上中间的“?”和结尾的“你”?

到目前为止,这连接了2个数组,但不确定如何将额外的字符添加到我想要的最后一个char变量中

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char foo[] = { "hello" };
    char test[] = { "how are" };
    strncat_s(foo, test, 12);
    cout << foo;
    return 0;
}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
char foo[]={“hello”};
字符测试[]={“你好”};
strncat_s(foo,test,12);

C++中的CUT< P>,使用<代码> STD::String ,以及 Trase+< /C> >,专门设计来解决这样的问题。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string foo( "hello" );
    string test( "how are" );
    cout << foo + " , " + test;
    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
串福(“你好”);
字符串测试(“你好”);

CUT

最好的用法是使用<代码> STD::C++中的字符串作为其他答案。如果你真的需要用char来工作,试试这个方法。没有测试。

const char* foo = "hello";
const char* test= "how are";

char* full_text;
full_text= malloc(strlen(foo)+strlen(test)+1); 
strcpy(full_text, foo ); 
strcat(full_text, test);

是的,在C++中使用<代码> +/>代码>操作器进行字符串连接。 但这是行不通的:

char[] + char[] + char[]
将一个数组转换为std::string,它将:

std::string(char[]) + char[] + char[]
例如:

#包括
int main()
{
常量字符a[]=“如何”;
常量字符b[]=“是”;
const char c[]=“你”;

std::cout如果您不想使用string,只需获取另一个数组并使用循环将这两个数组存储在其中即可

#include<iostream>
#include<stdlib.h>
#include<cstdio>
using namespace std;
int main(){
    char fname[30],lname[30],full_name[60];
    int i,j;
    i=0;j=0; // i is index of fname and j is index for lname
    cout<<"Enter your first name: ";
    gets(fname);
    cout<<"Enter your last name: ";
    gets(lname);
    for (i;fname[i]!='\0';i++){
        full_name[i] = fname[i];
    }
    cout<<"i ="<<i;
    full_name[i]=' ';
    i = i + 1;
    for (i,j;lname[j]!='\0';i++,j++){
        full_name[i] = lname[j];
    }
    cout<<"Your full name is: "<<full_name<<endl;
    system("pause");
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
char fname[30],lname[30],全名[60];
int i,j;
i=0;j=0;//i是fname的索引,j是lname的索引

coutUse
std::string
。这比使用数组(因为数组大小固定,所以根本不起作用)更容易。无关:您正在调用
#include
,但对std::string不做任何操作。如果您正在操作C样式的字符串,您可能至少应该包含
#include
。更好的解决方案可能是将输入转换为std::string类型,如果某些函数需要C字符串,请在std::string上调用
.C#u str
方法。为什么在stringxIs周围有大括号?使用arr的原因是什么表示字符串的
char
的天数?只是一个旁白-最好在输出后输出
'\n'
-在某些系统上,除非发送最后的换行符,否则程序退出后文本可能不可见(例如,一些UNIX/Linux shell-假设每个程序都将以换行符结束每一行-清除回行的开头,然后打印提示符)为什么你在C++代码中使用<代码> MalCal?也忘记了空的空格character@EdHeal“谢谢你,我添加了空字符。我必须问你,为什么<代码> Malc C/代码>在C++编译器中工作?因为它确实是原来的C++代码转换成C。但是对于新代码,你应该使用<代码>新< /代码>。还有为什么PRD?当您拥有C++的所有额外功能时,请使用C代码这种方法的问题是使用字符串,我需要连接2个字符。您始终可以从C样式字符串中构建
std::string
std::string myString(foo)
我在问题的编辑部分添加了我的发现,这是有效的,但这是最好的方法吗?请参阅
#include <iostream>

int main()
{
    const char a[] = "how ";
    const char b[] = "are ";
    const char c[] = "you ";

    std::cout << std::string( a + b + c ) << "\n"; // Error
    std::cout << std::string(a) + b + c  << "\n"; // Fine
}
#include<iostream>
#include<stdlib.h>
#include<cstdio>
using namespace std;
int main(){
    char fname[30],lname[30],full_name[60];
    int i,j;
    i=0;j=0; // i is index of fname and j is index for lname
    cout<<"Enter your first name: ";
    gets(fname);
    cout<<"Enter your last name: ";
    gets(lname);
    for (i;fname[i]!='\0';i++){
        full_name[i] = fname[i];
    }
    cout<<"i ="<<i;
    full_name[i]=' ';
    i = i + 1;
    for (i,j;lname[j]!='\0';i++,j++){
        full_name[i] = lname[j];
    }
    cout<<"Your full name is: "<<full_name<<endl;
    system("pause");
    return 0;
}