istringstream break_apart(input);
input = "";
while (break_apart >> word)
{
front = &word.at(0);
rear = &word.at(word.size() - 1);
while (front <= rear) {
swap (*front, *rear);
front++;
rear--;
}
input += word + " ";
}
Using this sort of method of pointers, how would I change this code to have my pointers point to an ENTIRE word, not just a character like I have above? Thank you very much for all of the help in advance!!
I would assume it would be something like that. @ writeonsharma, I just assumed you could achieve this goal by using pointers. I guess not, and what you said totally makes sense. So, if you can't use pointers to achieve this goal, what CAN i do to figure this out?! thanks!
No problem. If you had input "Hello to the world", I need a way of breaking up this string into an array word[](this is the best way I can think of it, there may be a better way), so that I can cout << word[0] would output "Hello", and so on and so forth. What I would like for the program to accomplish is when the user inputs "Hello to the world", I would like for the program to reverse the order of the words so that the output is "world the to Hello" or:
cout << word[3] << word[2] << word[1] << word[0]
Please let me know if that doesn't make sense. Thank you to all the help!!!
I've been messing with the portion of my code to accomplish this, and here is where I am. I realize that it's still outputting the same input, but I don't know how to reverse the order of the words
If someone can let me know what I can do to fix that, I would be greatly appreciative!!! Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13
istringstream break_phrase_apart(mod_input);
mod_input = "";
int count = 0;
while (break_phrase_apart >> word)
{
for (int k = count; k >= 0; k--)
{
mod_input += word + " ";
}
}
for (int x = 0; x < mod_input.length(); x++)
cout << mod_input[x];
OK, i fixed that part of my program, kinda, but now I have a new problem for some reason: My program will not output what I would like for it to be. This is weird, because it seems as if it would be the least of my worries, haha. I dunno. Even if I try to output something within a for loop toward the end of int main(), it doesn't for some odd reason...
If you all haven't been following this post, the object of the program is to input a string and then reverse the order of the words. Ex.) "Hello to the world!" --> "world! the to Hello"
The part I'm having trouble with (in bold) starts at the end of the sentence looking for spaces. When it finds a space, it copies each character to the output string between
the space and the end of the sentence, then updates to the new
location and looks for the next space.
Maybe someone can see what I'm not. Please make my program output something!!! Thanks for the help!!
Huh? You aren't trying to output anything. What do you mean? I see you trying to fill a character array with some data but where are you trying to output something?
This program:
1. reads in line of text
2. breaks it down into tokens (words)
3. saves each word into an array - counts as it goes - until the end of the line
4. prints out all the words in forward order (as a check)
5. prints out the words in reverse order
After executing the code with the string reverse the string, this while loop was stuck in a continuous loop. You should have been able to debug that yourself.
1 2 3 4 5
while (next_space_index > -1)
{
while (mod_input[next_space_index] != ' ' && next_space_index >= -1)
next_space_index--;
}
What do you suspect happens after the internal while loop ends and next_space_index has a value of 11? How will the outer loop ever end? Your program doesn't seem to be trying to reverse the words, you are simply reversing the characters within each word while leaving the words in their original places. As far as the last few loops in the program go, I can't figure out what you expect them to do. Go ahead and use strtok as mckenzie suggested provided that you are aware of the security problems associated with that solution. It is functional and it works. To be honest, this is much simpler than you seem to think. All you have to do is break the sentence into an array of std::string objects. Given the set of requirements you gave, it is really that simple. Then you can iterate forward and backwards through the string array printing each string forward and backwards. You don't really have to play games trying to reverse anything or manipulate any of the strings. Most of the stuff in this program is complete nonsense and totally unnecessary.