OK. Here is a working code (compiled and run it with notepad+command prompt just for you)
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string line("hey dude dad");
const char* cline = line.c_str();
string word("");
vector<string> words;
int size = line.size();
for (int i = 0; ;++i)
{
if (cline[i] == ' ') {
if (word != "")
words.push_back(word);
word = "";
continue;
}
if (cline[i] == '\n' || cline[i] == '\0') {
if (word != "")
words.push_back(word);
break;
}
word += cline[i];
}
cout << "WORDS" << endl;
for (int i = 0; i < words.size(); ++i)
cout << words[i] << endl;
}
The previous one had a lot of bugs. Some obvious some not. But I said it wasn't working.
A C++ version:
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string line("hey dude dad ");
string word;
vector<string> words;
int start = 0, end;
while ((end = line.find(' ', start)) != string::npos)
{
word = line.substr(start, end - start);
if (word != "")
words.push_back(word);
start = end + 1;
}
word = line.substr(start, line.size() - start);
if (word != "")
words.push_back(word);
cout << "WORDS" << endl;
for (int i = 0; i < words.size(); ++i)
cout << words[i] << endl;
}