Thread: convert string to char array

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    convert string to char array

    I'm trying to convert a string to a char array so that i can choose individual characters at a time. I've looked around but i can't seem to find a clear solution anywhere. So if anybody could give me an example or the command i need to use to do this, that would be great.

    Thanks,

    ...Dan

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you can access individual chars in an stl string...
    Code:
    std::cout<<str[1]<<std::endl;
    or you could convert it to a C-style string like so:
    Code:
    strcpy(target,str.c_str());
    just make sure your target is big enough to handle the incomming string.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Well, there is the

    basic_string::c_str() command, which returns a char array = to your basic string object..


    You could.... HMm...

    Create a char pointer..

    const char * cptr
    then do a cptr = basic_string object...

    There are probably a few more ways, but those are the highlights
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    System-7
    Join Date
    Nov 2005
    Posts
    65
    thanks, as usual, guys,

    ...Dan

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    just being nit-picky here but:

    c_str() is not a command, it's a member function.

    >> then do a cptr = basic_string object...

    although there was talk of adding an implicit conversion to const char*, it never made it to the standard. you have to use a member function or something like &str[0].
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Oopsies..

    I ment do a pptr = basic_string object



    I'm new at this helping thing
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    > I'm trying to convert a string to a char array so that i can choose individual characters at a time.
    if all you need is to choose individual chars at a time there is also string::at(int)
    ex..
    Code:
    string foo = "some string";
    for(int i=0; i<foo.length();++i) cout << foo.at(i);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 10:00 AM
  4. Replies: 10
    Last Post: 08-17-2005, 11:17 PM
  5. How to delete string after >
    By winsonlee in forum C Programming
    Replies: 5
    Last Post: 08-08-2004, 04:23 AM