Matimont
Matimont

Reputation: 759

Concatenate char arrays in C++

I have the following code and would like to end up with a char such as: "Hello, how are you?" (this is just an example of what I'm trying to achieve)

How can I concatenate the 2 char arrays plus adding the "," in the middle and the "you?" at the end?

So far this concatenates the 2 arrays but not sure how to add the additional characters to my final char variable I want to come up with.

#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;
}

EDIT:

This is what I came up with after all your replies. I'd like to know if this is the best approach?

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

int _tmain(int argc, _TCHAR* argv[])
{
    char foo[] = { "hola" };
    char test[] = { "test" };
    string foos, tests;
    foos = string(foo);
    tests = string(test);
    string concat = foos + "  " + tests;
    cout << concat;
    return 0;
}

Upvotes: 16

Views: 128980

Answers (7)

SkillFull_Shit
SkillFull_Shit

Reputation: 1

FOR 2 CHAR ARRAYS

  char foo[] = { "hello " };
  char test[] = { "how are" };
  char concat[50];
  char x='X'; //Any Temporary Variable
  int i;
  for(i=0; x!='\0'; i++){ //x is not NULL
    x = foo[i];
    concat[i] = x;
  }
  x = 'D';
  i--;
  for (int k = 0; x!='\0'; i++){
    x = test[k];
    concat[i] = x;
    k++;
  }
  cout<<"Concat Array is: "<<concat;

Upvotes: -2

Mrityunjay Tripathi
Mrityunjay Tripathi

Reputation: 19

If performance is a concern for you, I would suggest avoiding std::string. Instead, you can use the character array.

template <typename Result>
void concatenate(Result *res)
{
  return;
}

template <typename Result, typename T>
void concatenate(Result *res, T *str)
{
  strcat(res, str);
}

template <typename Result, typename First, typename ... T>
void concatenate(Result *res, First *f, T* ... next)
{
  strcat(res, f);
  concatenate(res, next...);
}

template <typename Result, typename First, typename ... T>
void concatStrings(Result *res, First *f, T* ... next)
{
  strcpy(res, f);
  concatenate(res, next...);
}

And then, you can call the concatStrings function with at least two parameters and at most as many you need.

/* You can remove constexpr as per your need. */
constexpr char hello[6] = "hello";
constexpr char sep[2] = ",";
constexpr char how[5] = " how";
constexpr char are[5] = " are";
constexpr char you[6] = " you?";

auto totalSize = strlen(hello) + strlen(sep) + strlen(how) + strlen(are) + strlen(you) + 5;

char statement[totalSize];
concatStrings(statement, hello, sep, how, are, you);
std::cout << statement << '\n';

Upvotes: 1

Yash Dhanlobhe
Yash Dhanlobhe

Reputation: 25

cout<<x<<y<<z<<" ";
char arr[3] = {x , y ,z};
ans.push_back(arr);

if you want to push in vector array.

Upvotes: -1

Noman
Noman

Reputation: 53

If you dont want to use string you can do it simply by taking an other array and storing both arrays in it with loop

#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;
}

Upvotes: 2

jav
jav

Reputation: 674

Yes, in C++ use the + operator for string concatenation. But this will not work:

char[] + char[] + char[]

convert one array to std::string and it will:

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

E.g.:

#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
}

Upvotes: 3

Nayana Adassuriya
Nayana Adassuriya

Reputation: 24766

Best thing is use std::string in C++ as other answers. If you really need to work with char try this way. didn't tested.

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);

Upvotes: 8

quantdev
quantdev

Reputation: 23813

In C++, use std::string, and the operator+, it is designed specifically to solve problems like this.

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

int main()
{
    string foo( "hello" );
    string test( "how are" );
    cout << foo + " , " + test;
    return 0;
}

Upvotes: 13

Related Questions