[Solved] take a word from the user and prints it as shown below. Enter the word: Word d rd ord Word [closed]


To get the expected output, you can make the outer loop loop from word.length() - 1 down to 0 instead:

#include <cstddef>   // std::size_t
#include <iostream>
#include <string>

int main() {
    std::cout << "Enter the word:";

    if (std::string word; std::cin >> word) {
        for (auto i = word.length(); i--;) {                   // <- here
            for (std::size_t j = i; j < word.length(); j++) {
                std::cout << word[j];
            }
            std::cout << '\n';
        }
    }
}

Note that I changed i and j to be (unsigned) std::size_ts instead of (signed) int. std::size_t is the correct type to use with the length() member function.

You could also simplify it by skipping the inner loop completely:

        for (auto i = word.length(); i--;) {
            // supply a char* where to start printing from:
            std::cout << &word[i] << '\n';
        }

solved take a word from the user and prints it as shown below. Enter the word: Word d rd ord Word [closed]