spliting strings

This is a discussion on spliting strings within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm working on a program where i need to enter a string of numbers and then seperate the numbers ...

  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
    1,600
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    better to use stringstream over the strtok, that has some issues
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    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,021
    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
    21,461
    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));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07: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

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