Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- int main()
- {
- std::string input = " know me? do you ";
- int count = 0;
- //
- // get words
- //
- std::vector<std::string> words;
- std::string word;
- size_t length = input.length();
- for (size_t i = 0; i < length; ++i)
- {
- if (input[i] != ' ' && i != length - 1)
- {
- word += input[i];
- }
- else
- {
- if (word.length() > 0)
- {
- words.push_back(word);
- word.clear();
- count++;
- }
- }
- }
- //
- // print words
- //
- std::cout << count << std::endl;
- for (std::vector<std::string>::iterator it = words.begin(); it != words.end(); it++)
- {
- std::cout << *it << std::endl;
- }
- std::cout << "===" << std::endl;
- //
- // permutate vector
- //
- int firstWord = 2;
- int lastWord = 3;
- int repeat = 0;
- while (repeat < lastWord - firstWord + 1)
- {
- for (int i = lastWord; i > 0; i--)
- {
- std::swap(words[i], words[i - 1]);
- }
- repeat++;
- }
- //
- // print permutated vector
- //
- for (std::vector<std::string>::iterator it = words.begin(); it != words.end(); it++)
- {
- std::cout << *it << std::endl;
- }
- //
- // build & print new string
- //
- std::string result;
- for (std::vector<std::string>::iterator it = words.begin(); it != words.end(); it++)
- {
- if (it != words.begin())
- {
- result += ' ';
- }
- result += *it;
- }
- std::cout << "result: " << result << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement