26

I am trying to insert a string separated by spaces into an array of strings without using vector in C++. For example:

using namespace std;
int main() {
    string line = "test one two three.";
    string arr[4];

    //codes here to put each word in string line into string array arr
    for(int i = 0; i < 4; i++) {
        cout << arr[i] << endl;
    }
}

I want the output to be:

test
one
two
three.

I know there are already other questions asking string > array in C++, but I could not find any answer satisfying my conditions: splitting a string into an array WITHOUT using vector.

Cœur
  • 34,719
  • 24
  • 185
  • 251
txp111030
  • 285
  • 1
  • 4
  • 11
  • How would you go about printing each word on a separate line to begin with? – X-Istence Apr 16 '13 at 05:22
  • already answerd look here http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c – Lars Apr 16 '13 at 05:38
  • Here are some similar questions: http://stackoverflow.com/questions/1894886/parsing-a-comma-delimited-stdstring http://stackoverflow.com/questions/1321137/convert-string-containing-several-numbers-into-integers http://stackoverflow.com/questions/1141741/int-tokenizer http://stackoverflow.com/questions/3162108/a-better-way-to-split-a-string-into-an-array-of-strings-in-c-c-using-whitespac http://stackoverflow.com/questions/1323824/how-to-read-numbers-from-an-ascii-file-c –  Apr 16 '13 at 06:27

5 Answers5

48

It is possible to turn the string into a stream by using the std::stringstream class (its constructor takes a string as parameter). Once it's built, you can use the >> operator on it (like on regular file based streams), which will extract, or tokenize word from it:

#include <iostream>
#include <sstream>

using namespace std;

int main(){
    string line = "test one two three.";
    string arr[4];
    int i = 0;
    stringstream ssin(line);
    while (ssin.good() && i < 4){
        ssin >> arr[i];
        ++i;
    }
    for(i = 0; i < 4; i++){
        cout << arr[i] << endl;
    }
}
Ryan Knutson
  • 76
  • 2
  • 14
didierc
  • 14,377
  • 3
  • 31
  • 51
  • 3
    This answer is very simple, to the point, and more importantly works! Thank you very much! – txp111030 Apr 16 '13 at 05:49
  • It splits the individual characters for me. – Krii Feb 26 '15 at 20:02
  • Hymm, if there is a space at the end of the "line" variable. And you remove the <4 and use push_back to store in a vector, then you get the last element twice. There's something wrong with this code. – Owl Feb 13 '16 at 16:43
4
#include <iostream>
#include <sstream>
#include <iterator>
#include <string>

using namespace std;

template <size_t N>
void splitString(string (&arr)[N], string str)
{
    int n = 0;
    istringstream iss(str);
    for (auto it = istream_iterator<string>(iss); it != istream_iterator<string>() && n < N; ++it, ++n)
        arr[n] = *it;
}

int main()
{
    string line = "test one two three.";
    string arr[4];

    splitString(arr, line);

    for (int i = 0; i < 4; i++)
       cout << arr[i] << endl;
}
Alexey
  • 41
  • 2
2
#define MAXSPACE 25

string line =  "test one two three.";
string arr[MAXSPACE];
string search = " ";
int spacePos;
int currPos = 0;
int k = 0;
int prevPos = 0;

do
{

    spacePos = line.find(search,currPos);

    if(spacePos >= 0)
    {

        currPos = spacePos;
        arr[k] = line.substr(prevPos, currPos - prevPos);
        currPos++;
        prevPos = currPos;
        k++;
    }


}while( spacePos >= 0);

arr[k] = line.substr(prevPos,line.length());

for(int i = 0; i < k; i++)
{
   cout << arr[i] << endl;
}
Vijendra Singh
  • 628
  • 3
  • 11
  • `string::find` returns `string::npos` if it did not find the string being searched and not 0. – RedX Apr 16 '13 at 07:11
  • std::string::npos is a static member constant value with the greatest possible value for an element of type size_t. This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type. – Vijendra Singh Apr 16 '13 at 07:17
0

Here's a suggestion: use two indices into the string, say start and end. start points to the first character of the next string to extract, end points to the character after the last one belonging to the next string to extract. start starts at zero, end gets the position of the first char after start. Then you take the string between [start..end) and add that to your array. You keep going until you hit the end of the string.

Frerich Raabe
  • 86,449
  • 18
  • 111
  • 204
0
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main() {

    string s1="split on     whitespace";
    istringstream iss(s1);
    vector<string> result;
    for(string s;iss>>s;)
        result.push_back(s);
    int n=result.size();
    for(int i=0;i<n;i++)
        cout<<result[i]<<endl;
    return 0;
}

Output:-

split
on
whitespace

  • 1
    From Review:  Hi, please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Nov 08 '19 at 18:05