Created
June 8, 2012 03:40
-
-
Save grodtron/2893404 to your computer and use it in GitHub Desktop.
A small program to reverse the order of words in a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
/* | |
* | |
* Small program to reverse the order of words in a sentence | |
* | |
* */ | |
string * reverseSentence(const string & input){ | |
// This will be the return value | |
// | |
// We resize it to the exact same length as the input. | |
// We're not changing the characters in the string, only | |
// their order | |
string * output = new string; | |
output->resize(input.length()); | |
// Iterators to the input string. | |
// | |
// i_it -- This is the iterator that is going to be constantly | |
// moving along the input string | |
// | |
// i_w_it -- This iterator is going to be set to the beginning of | |
// words as they are found | |
// | |
// i_end -- This is just checked against so we don't go past the end | |
string::const_iterator i_it = input.begin(); | |
string::const_iterator i_w_it = input.begin(); | |
string::const_iterator i_end= input.end(); | |
// Iterator to the end of the output string. This is going to walk | |
// backwards down the output as the input iterator walks up the input | |
// string. This iterator will always be the position in the output | |
// where the next word should be copied to | |
string::iterator o_it = output->end(); | |
while(i_it != i_end){ | |
// Skip to the end of the current word, decrementing the output | |
// while simultaneously decrementing the output iterator the right | |
// amount of positions | |
while(i_it != i_end && *i_it != ' ') ++i_it, --o_it; | |
// The state of the iterators should now be: | |
// | |
// i_it = right after the end of the current word | |
// | |
// i_w_it = the beginning of the current word | |
// | |
// o_it = the position in the output where the next | |
// word should be inserted. | |
// | |
// | |
// At this point we use the three iterators described above to | |
// copy the current word into the output. | |
copy(i_w_it, i_it, o_it); | |
// if we're not at the end of the input, then we insert a space | |
// into the output and update i_w_it, | |
if(i_it != i_end){ | |
*(--o_it) = ' '; | |
++i_it; | |
i_w_it = i_it; | |
} | |
// that's all, folks | |
} | |
return output; | |
} | |
int main(int argc, const char *argv[]) | |
{ | |
// Get input | |
char buff[1024]; | |
cout << "Enter the string to be reversed" << endl << ">>> "; | |
cin.getline(buff, 1024, '\n'); | |
string input(buff); | |
// Produce and print output | |
string * output = reverseSentence(input); | |
cout << "input : \"" << input << '"' << endl | |
<< "output: \"" << *output << '"' << endl; | |
delete output; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment