For instance...

say that I set

char a = 'a'
char b = 'b'

how would I combine them to make them "ab" using strcat?

Recommended Answers

All 2 Replies

You can't because both those functions only work on null-terminated strings. In your example, neither a nor b has enough room to hold more than one character. Then could be contantinated like this:

char a = 'a';
char b = 'b';
char all[3];
all[0] = a;
all[1] = b;
all[2] = '\0';

Well, in your example you can't, because strcat requires two char*, whereas a and b are simple chars. What you can do is to work with strings:

#include <iostream>
#include <string>
using namespace std;
int main(){
   char a='a';
   char b='b';
   const char* all;
   string together;
   together+=a;
   together+=b;
   all=together.c_str();
   cout<<all;
   return (0);
}

or

int main(){
    char a='a';
    char b='b';
    string together;
    together.insert(together.begin(), a);
    together.insert(together.begin()+1, b);
    cout<<together;
    return (0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.