Thread: Turn a string into an array

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    15

    Turn a string into an array

    I'm kind of new to C++ and I want to turn a string into an array. I want to separate each element of the array by detecting the "+" signs in the string. I know there is a function that can do this, but I just can't remember what it is.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I'm not sure if you are aware of this.. but <string> class variables can be accessed like arrays...

    example:
    Code:
    string first_name = "Jose";
    
    char first_initial = first_name[0];

    so basically, a string class variable are just an array of characters.
    Last edited by The Brain; 12-22-2004 at 11:27 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    No, no, no. Let's say I have this string: "Joe+Dan+Tom"

    I want to create an array with Joe Dan and Tom as the elements of it. I want to create an array from the string by separating the elements with the plus sign.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    If you put that string into a stringstream, you can use the getline function with '+' as the delimiter.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    strtok perhaps?

    #include <string.h>
    char *strtok(char *s1, const char *s2);

    Description:

    Searches one string for tokens, which are separated by delimiters defined in a second string.

    strtok considers the string s1 to consist of a sequence of zero or more text tokens, separated by spans of one or more characters from the separator string s2.

    The first call to strtok returns a pointer to the first character of the first token in s1 and writes a null character into s1 immediately following the returned token. Subsequent calls with null for the first argument will work through the string s1 in this way, until no tokens remain.

    The separator string, s2, can be different from call to call.

    Note:

    Calls to strtok cannot be nested with a function call that also uses strtok. Doing so will causes an endless loop.

    Return Value:

    strtok returns a pointer to the token found in s1. A NULL pointer is returned when there are no more tokens.
    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
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    Umm...how would I do that? Heh, sorry, as I said, I'm pretty new to C++. I only know PHP, so I am basically thinking of a function like explode() in PHP. Is there a function like that in C++?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'd stick to C++ strings, and use the "find" and "substr" methods which come with that class.
    http://www.bgsu.edu/departments/comp...cs/string.html

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    That made no sense to me, can anyone else help?

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok so you want to call explode() on a string and get back an array of strings right?

    So you should want as a return from your explode function a vector<string>
    You should pass your explode function a string or string & and a delimiter character.

    Inside your explode function you'll have to call find to find the the occurances of the delimiter character. As you find them you simply call substr to pull out the piece you want and then push_back it into the vector. Once you are done parsing the string then you return the vector.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM