arrays & strings

I have to swap the internal characters of words after using getline. I have the following code for changing the words:

string word;
for ( i = 1; i < word.length() - i; i++ ) {
word[i] = word[word.length() - i];
}
word = string new_word;

it should operate like this:
input: word
output: wrod
except with a sentence.
Last edited on
What is the question?
Array (and string) indexing starts at 0, (not 1), by the way.
make use of an std::vector. Push each word on the the vector. At the end of the sentence, itterate over the vector from end to the begining and then swap each letter as in your existing routine. That way, the entire sentence is backwards.
@Athar
It starts at 1 instead of zero because the first and last characters of the string have to stay the same, and only the internal characters are swapped around.

The question is how do I utilize the function I gave above to function for a sentence.

@Barryso, I didn't think of that, thanks for the help.

Also isn't there a function that can look ahead at the next character( I think it is called peek or something like that), so that I could check to see if the next character is whitespace or a period to determine the words to put into a vector of the words.
Topic archived. No new replies allowed.