-1

I need to get string array from string line. It is possible ?

main(){
 string x = "This is string";

 //do something, function or... i don't know...
 //result

 cout << string[0] << string[1] << string[2] << endl;

 //cout << "This" << "is" << "string" << endl;
}

How to do this array ??! Ty ;)

bakis
  • 626
  • 3
  • 8
  • 22

2 Answers2

1

You can tokenize a string into a container of strings like this:

string data = "quick brown fox jumps over the lazy dog";
stringstream input(data);
vector<string> res;
copy(
    istream_iterator<string>(input)
,   istream_iterator<string>()
,   back_inserter(res));

demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

For this use std:string:substr like this

string = "This is string";
string2 = string.substr(8,6) //string2 now equals "string"

the 8 refers to the position of the first character

the 6 refers to number of characters from that point

ensure you have #include <string> in your declerations

more info on std:string:substr here

Luke
  • 317
  • 2
  • 6
  • 17