Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // WK Shearer Oct 2012.
- // Simple console application, C++.
- // Sample command interface, like a receptor for text based adventure game.
- // Takes a command, splits it into words, eliminates blanks and spaces, and
- // converts the words to upper case for later comparison to verb / noun bank.
- #include <iostream>
- #include <string>
- #include <cctype>
- #include <vector>
- using namespace std;
- int main()
- {
- string mystr;
- char search = ' ';
- string sub_str;
- vector<string> words;
- cout << "Enter command: ";
- getline(cin, mystr);
- cout << "Command is " << mystr << endl;
- //cout << mystr.length() << endl;
- size_t i = 0, j = 0;
- for(i = 0; i < mystr.size(); i++)
- {
- if(mystr.at(i) != search)
- {
- sub_str.insert(sub_str.end(), mystr.at(i));
- }
- if(i == (mystr.size() - 1))
- {
- words.push_back(sub_str);
- sub_str.clear();
- }
- if(mystr.at(i) == search)
- {
- words.push_back(sub_str);
- sub_str.clear();
- }
- }
- // clean out any accidental blank words (extra spaces)...
- //cout << "initial words.size() " << words.size() << endl;
- for(i = words.size() - 1; i > 0; i--)
- {
- if(words.at(i) == "")
- words.erase(words.begin() + i);
- }
- //cout << "words.size() after clean up " << words.size() << endl;
- for(i = 0; i < words.size(); i++)
- {
- cout << words.at(i) << endl;
- }
- // Make words upper case
- for(i = 0; i < words.size(); i++)
- {
- for(j = 0; j < words.at(i).size(); j++)
- {
- if(islower(words.at(i).at(j)))
- words.at(i).at(j) = toupper(words.at(i).at(j));
- }
- }
- for(i = 0; i < words.size(); i++)
- {
- cout << words.at(i) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement