How to convert a string to a character array?

This is a discussion on How to convert a string to a character array? within the C++ Programming forums, part of the General Programming Boards category; I am having some problem here. let' say that string c="Hello"; char a[5]; how can i convert the string to ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    8

    How to convert a string to a character array?

    I am having some problem here.
    let' say that

    string c="Hello";
    char a[5];

    how can i convert the string to a character array? Urgent

  2. #2
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    you could increment your string and place all individually indexed location of the string into each individual location of the character array. you could do it with one loop, a string, and a char array. i unfortunatly do not recall any premade function to do this instantly.
    Last edited by xviddivxoggmp3; 04-17-2005 at 11:01 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    The string class contains a function that returns the string as a character array:

    Code:
    string c="Hello";
    strcat(a, c.c_str());   // the character array a will now contain "Hello"

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    8
    thank you

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Code:
    #include <iostream>
    #include <cstring>
    #include <string>
    
    int main()
    {
        std::string str1 = "Hello";
        char str2[str1.length() + 1];
        //we can't just use length that would result in not null terminating 
        std::strcpy(str2,str1.c_str());
        
        std::cout<<"str1.length() = "<<str1.length()<<std::endl;//here is proof :)
        std::cout<<"str1 = "<<str1<<std::endl;
        std::cout<<"str2 = "<<str2<<std::endl;
        
        std::cin.get();
        return 0;
    }
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Navigating a character string array from pointers
    By Boxknife in forum C Programming
    Replies: 3
    Last Post: 04-16-2009, 01:32 PM
  2. How to put a string in a 2 dimensional character array?
    By atif7865 in forum C Programming
    Replies: 2
    Last Post: 12-05-2008, 09:26 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21