0

For instance...

say that I set

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

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

3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by Lucaci Andrew
0

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

Edited by Ancient Dragon

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

Edited by Lucaci Andrew

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.