split char array and store in vector

I've searched everywhere and can't find a way to split a char array by space (" ") and store each word into a vector.

For example, user inputs "House 400", splitInput [0] = House, splitInput [1] = 400.

...

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
string input;
vector <string> splitInput;

getline(cin, input);

char* chararray = new char[input.length() + 1]; 
strcpy_s(chararray, input.length() + 1, input.c_str());

//code to split chararray by space and store into splitInput

}
Last edited on
don't use char array if you can use string.
strtok may do what you want.
if not you can do it by hand by copying.

more or less:
1
2
3
4
5
6
7
8
9
10
for(all the letters in the char array)
{
    if(array[index] != ' ')
      vec[currentword][currentposition++] = array[index];
     else
 {
      vec[currentword++][currentposition] = 0;
      currentposition = 0;  
 }
}


but it would be much easier if you could use substrings or other c++ tools instead of the c-string nonsense.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <vector>
#include <cctype>

std::vector<std::string>
split(const std::string& line) {
    std::vector<std::string> tokens;
    size_t i = 0;
    while (i < line.size()) {
        for ( ; i < line.size() && isspace(line[i]); ++i) ;
        if (i >= line.size())
            break;
        size_t start = i;
        for ( ; i < line.size() && !isspace(line[i]); ++i) ;
        tokens.push_back(line.substr(start, i - start));
    }
    return tokens;
}

int main() {
    std::string line;
    std::getline(std::cin, line);
    auto tokens = split(line);
    for (const auto& tok: tokens)
        std::cout << '[' << tok << "]\n";
}

As jonnin points out you are already using std::string to get your input, use std::string as your vector type.

With spaces as your word delimiters parsing out individual words using a std::stringstream is one way to do it.

https://en.cppreference.com/w/cpp/io/basic_stringstream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

int main()
{
   // create an empty vector
   std::vector<std::string> strvec;

   std::string input;

   std::cout << "Enter a sentence:\n";
   std::getline(std::cin, input);
   std::cout << '\n';

   std::stringstream strbuf { input };

   std::string temp;

   while (strbuf >> temp)
   {
      strvec.push_back(temp);
   }

   std::cout << "The string vector's size: " << strvec.size() << '\n';

   std::cout << "The string vector contains:\n";

   for (const auto& itr : strvec)
   {
      std::cout << itr << '\n';
   }
}

Enter a sentence:
this is a test of a string stream

The string vector's size: 8
The string vector contains:
this
is
a
test
of
a
string
stream


If you need a C string (char array) from the elements use the string's c_str() member function.

https://en.cppreference.com/w/cpp/string/basic_string/c_str
I feel dirty but here is the hands-on C-string code. Try not to do stuff like this.

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

int main()
{  
   char c[] = "the quick brown fox 1234";
   int l = strlen(c);
   vector<char[100]> vc(20); //you can use a push-back somewhere if you don't want to do this up front.  The extras print extra lines below, but this isnt exactly code of the year anyway. 
   int w = 0;
   int p = 0;
   for(int i = 0; i <= l; i++)
   {
	if(c[i] != ' ' && c[i])
      vc[w][p++] = c[i];
    else
	{
		  vc[w++][p] = 0;
		  p = 0;
	}		
	   
   }
   for(int i = 0; i < vc.size(); i++)
    cout << vc[i] << endl;
}
Topic archived. No new replies allowed.