Please help its urgent i wrote this program but the output is not right. The program is not inversing the order of the words in the string. for example: input: i love school
output: school love i . Please help thanks

#include<iostream.h>
#include<string>
using namespace std;

main() {
     std::cout << "Please enter a sequence of words: ";
     std::string y;  // read words and write them one per line
     char lcv;
   
void reverseArrayElement( string data[], int begin, int end); // reverse the elements of a string array
 while(cin >> y) {
 std::cout << y[lcv] << endl;}


//    begin is the index of the first element in the range to be reversed, and end is the index after the last element in the range

system("Pause");

}

You've got some sense of the problem, but your code is incorrect.

You declare y as a string on line 7, but treat it like an array of strings on line 12. In addition, your index of that array is a char, which could be technically correct, but, for example, y would equal y[65] and that's not what you want. Keep it as an array, but use a for loop to step through it in reverse.

There are other issues, too. <iostream.h> should just be <iostream> (you may need the .h if you have a prehistoric compiler from before the standard, but you are using <string>, so I'm not convinced that is the case. Also, main needs to explicitly return an int. The way you have it the int is implied, and this is also a pre-standard vestige.

You've got some sense of the problem, but your code is incorrect.

You declare y as a string on line 7, but treat it like an array of strings on line 12. In addition, your index of that array is a char, which could be technically correct, but, for example, y would equal y[65] and that's not what you want. Keep it as an array, but use a for loop to step through it in reverse.

There are other issues, too. <iostream.h> should just be <iostream> (you may need the .h if you have a prehistoric compiler from before the standard, but you are using <string>, so I'm not convinced that is the case. Also, main needs to explicitly return an int. The way you have it the int is implied, and this is also a pre-standard vestige.

Please can you give me an example of where i can put the for loop because i tried it but it crashes and also how do i keep it as an array !! please help i am not very good at c++

Hi,
If you read each character into an array and the string length into another. You can then read them back using a for loop and iterating down from the string length back to zero.
Don't forget to take 1 off the string length otherwise you get a blank at the start which is the null character printing out.

Regards

1. Take the input from the user into a string called input
2. Use string length to go to the end of the string.
3a. Start iterating towards the start of the string. When you hit the space char, then stop. Store the chars that you iterated over into a temp string.
b. Reverse the temp string and then store the output in the output string
4. Repeat step 3 till you cover the entire input string

This article has been dead for over six months. Start a new discussion instead.