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");

}

Dani AI

Generated

Quick diagnosis: is treating a single std::string like an array of strings and using an uninitialized char as an index. That invokes undefined behavior (garbage characters, crashes, strange punctuation like the extra dot). was right to call out the types — fix the types and let the compiler help by using <iostream> and an explicit int main() so you get useful warnings.

A clean, robust solution is: read the full input line with std::getline, split the line into whitespace-separated words (use std::istringstream and operator>>) and push each token into a std::vector<std::string>. Then print the vector in reverse (reverse loop or reverse iterators). This avoids fixed-size arrays and out-of-bounds access. If you need to preserve or move punctuation (e.g., keep a trailing period at the end), handle that as an extra parsing step after splitting.

Example implementation:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main() {
    std::string line, word;
    if (!std::getline(std::cin, line)) return 0;
    std::istringstream iss(line);
    std::vector<std::string> words;
    while (iss >> word) words.push_back(word);
    for (std::vector<std::string>::reverse_iterator it = words.rbegin(); it != words.rend(); ++it) {
        if (it != words.rbegin()) std::cout << ' ';
        std::cout << *it;
    }
    std::cout << '\n';
    return 0;
}

If you still see crashes, compile with warnings (g++ -Wall -Wextra), check for uninitialized variables (that lcv), and run a debugger or valgrind to catch UB. ’s suggestion to iterate backwards is fine once words are stored correctly, and ’s stepwise splitting matches this approach — using a vector plus reverse iteration keeps the code simple and safe.

Recommended Answers

All 4 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.