Skip to content

Instantly share code, notes, and snippets.

@mikehow1984
Created August 8, 2015 13:41
Show Gist options
  • Save mikehow1984/6ed1ca3037d8772818dd to your computer and use it in GitHub Desktop.
Save mikehow1984/6ed1ca3037d8772818dd to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cctype>
#include <vector>
#include <string>
using namespace std;
vector<string> rotator(const string& s)
{
vector<string> word;
typedef string::size_type str_sz;
str_sz i = 0;
str_sz j = 0;
int word_count = 0;
//------word count loop-----------------------------
while (i != s.size())
{
while (i != s.size() && isspace(s[i]))
{
++i;
}
j = i;
while (j != s.size() && !isspace(s[j]))
{
j++;
}
i = j;
++word_count;
}
string sp = s;
sp += " ";
word.push_back(sp + '\n');
//-----------------------------------------------------
//-----------------rotator loop----------
for (int k = 0; k < word_count-1; ++k) //using the number of words, breaks off the last word
{ //and reattaches it to the front of the string
i = 0;
j = 0;
while (!isspace(sp[j]))
{
++j;
}
word.push_back(sp.substr (j+1, sp.size()) + sp.substr (i, j) + " " + '\n');
sp = sp.substr (j+1, sp.size()) + sp.substr (i, j) + " ";
}
//------------------------------------------
return word;
}
int main()
{
string sentence;
cout << "Enter a sentence: ";
while(getline(cin, sentence))
{
vector<string> v = rotator(sentence);
for(vector<string>::size_type i = 0; i != v.size(); ++i)
{
cout << v[i];
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment