Now you could break the sentence up into the separate words or you could use the space as the delimiter. Reverse can work for you even if you keep the entire sentence in one string. std::string provides substr and find functions to help you with that. Use find to find the iterator pointing to the space. You can then have a start and stop iterator for each word that can be used with std::reverse.
#include <iostream>
#include <algorithm>
#include <string>
int main()
{
// create a string
std::string strObject("Hello to the whole world ");
// initialize iterator and offset
std::string::iterator start = strObject.begin();
std::string::iterator space = strObject.begin();
std::string::iterator end = strObject.end();
// reverse each word
while(end != (space = std::find(start, end, ' ')))
{
// after the call, increment the iterator and
// copy it to the new start
std::reverse(start, space);
start = ++space; // move past the space
} // end while. ends when no more spaces are found
// reverse the last word unless the sentence ended with a ' '
if(start != strObject.end())
{
std::reverse(start, end);
}
// print the result
std::cout << strObject << std::endl;
return 0;
}
I had an itch to write some code so I just did part of it. You still have to figure out the user input. I just realized that I hate std::string::find. I don't understand why they coded it to return a position as an integer rather than an iterator. I sure wish that they would have overloaded the find but maybe it's because the std::find already does that.
ok, so im not gonna use reverse, cuz that's kinda cheap. so what i want to do, (use my above example) is kinda like a palindrome tester. You take one word, you assign the first character to a pointer variable and you assign the last character of that SAME word a different character, and then you work inwards. can any one get me started please? Sorry kempofighter, I don't really understand your example that much, but I appreciate the ideas.
You are on the right track with your palindrome idea. std::reverse() used std::swap() to switch the first and last characters, working inward until the iterators (pointers) meet.
I'm sure there are many ways of doing this. It sounds like you are asking us, "what is the most primitive and overly complex way to do something that has already been done more simply?" Perhaps you should quit using c++ libraries completely and just use char* and c run time libraries. I'm sure that you can follow my original concept without iterators. You can use operator[] with character arrays. If you fiddle with the for loops you can do it that way character by character. Study the example provided by the std::reverse example on this website and you can do the same thing with operator[].
By closing do you mean error closing? If yes, then may be there is no word either at word.at(0) or word.at(word.size() - 1); and may be it is accessing some garbage address. So when you try to dereference it, it breaks. Depending on at which cout it is breaking. You can try to debug it using gdb and check what exactly are the contents of word.at(0) and word.at(word.size() - 1)
But if it is a normal termination then word.at(0) and word.at(word.size() - 1); are either NULL or may be space character.
well, here is the code that i have so far, excluding all of the given stuff. it all works and compiles as expected, until this point where it just closes the running problem and claims there is an error. would this be a run-time error?! i don't know. maybe you might know what is wrong... thanks for the help!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
string input, word;
char *front, *rear;
vector <string> string; // This will hold the individual words temporarily until they are replaced
cout << "Enter a sentence that you would like to reverse: ";
getline(cin, input);
// Puts the string into a stream
istringstream break_apart(input);
while (break_apart >> word)
{
*front = word.at(0);
cout << *front;
*rear = word.at(word.size() - 1);
cout << *rear;
string.push_back(word);
}