Thread: spliting strings

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    spliting strings

    Hey,
    I'm working on a program where i need to enter a string of numbers and then seperate the numbers by the at every space.
    Example:
    input "103 105 235" then i want something like an array with array[0] = 103, array[1] = 105, ect.
    any help would be greatly appricated

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    do you want the numbers as strings or integers?

    i.e. array[0] = 103 or array[0] = "103"

    I'd look at the stringstream class
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    First get the amount of spaces, then make an array of that size + 1. Then loop through the inputed string, throwing chars into a temp buffer. Once you hit a space, convert the temp buffer into an int, and put it in the array, and so on and so forth. Btw, this sounds like homework.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Yarin View Post
    First get the amount of spaces, then make an array of that size + 1. Then loop through the inputed string, throwing chars into a temp buffer. Once you hit a space, convert the temp buffer into an int, and put it in the array, and so on and so forth. Btw, this sounds like homework.
    meh, too much like work. use a stringstream.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Code:
    char line[] = "103 56 73 89";
    int numbers[99];
    int numbCount = 0;
    char* word = strtok(line, " ");
    while(word != NULL && numbCount < 99)
    {
      numbers[numbCount] = atoi(word);
      word = strtok(NULL, " ");
      numbCount++;
    }

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    better to use stringstream over the strtok, that has some issues
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Code:
    std::string input("103 56 73 89");
    std::istringstream s(input);
    std::vector<int> nums;
    int temp;
    while(s >> temp) nums.push_back(temp);
    or maybe if you prefer
    Code:
    int* nums;
    std::string input = "103 56 73 89";
    int elements = std::count(input.begin(),input.end(), ' ') + 1;
    num = new int[elements];
    std::istringstream s(input);
    for(int x = 0; x < elements, ++x) s >> nums[x];
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You don't need to use a stringstream if the input is already from a stream like standard input. For example:

    Code:
    vector<int> targetArray;
    copy( istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(targetArray) );
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One could also eliminate the explicit loop in the former example by using std::copy(), e.g.,
    Code:
    std::string input("103 56 73 89");
    std::istringstream s(input);
    std::vector<int> nums;
    std::copy(std::istream_iterator<int>(s), std::istream_iterator<int>(),
              std::back_inserter(nums));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Spliting strings?
    By bladerunner627 in forum C++ Programming
    Replies: 19
    Last Post: 09-28-2005, 02:45 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM