Thread: strings and char[]

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Cool strings and char[]

    Hi there!!!!!!!!,

    I have two c type char arrays that I want to add together (with a space in between) and put into a cpp string. I am assuming da dis is the best way 2 do it.

    eg)

    Code:
    char n1[10] = "Bill";
    char n2[10] = "Stone"
    string str = n2 + " " + n1;
    I appreciate that stirng can be added but not like this.

    Anyone have any ideas for me plz?

    Thanks!!!

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Use the strcat function.

    Or you can use:
    sprintf(fullbuffer, "%s %s", n2, n1);
    Just make sure you have a big enough buffer.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or, if you want to actually use C++ style strings from C style array strings:
    Code:
    string str = string(n1) + " " + string(n2);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Code:
    stringstream strs << n1 << " " << n2;
    string str = strs.str();

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Lol, you guys taking the easy way out !
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    Quote Originally Posted by matsp View Post
    Or, if you want to actually use C++ style strings from C style array strings:
    Code:
    string str = string(n1) + " " + string(n2);
    --
    Mats
    Brilliant!!!!!!!!!!!!!!!!

    Would that be classed as type casting the char *'s??????

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That would be creating a temporary string out of a char* using the respective constructor.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-16-2008, 12:15 AM
  2. Strings and tokenizing need help
    By ksaman in forum C Programming
    Replies: 9
    Last Post: 02-04-2008, 06:15 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. malloc with arrays of strings
    By Lib in forum C Programming
    Replies: 2
    Last Post: 08-03-2003, 10:46 PM
  5. Replies: 18
    Last Post: 06-21-2003, 10:57 AM