Write a program that accepts a strong of character from the user. The program should display the characters in reverse order. In other words, if the user enters the string "Programming", the program should display "gnimmargorP".
I don't even know were to begin with this code. Would appreciate any help I can get.
cout << "Enter characters or word: ";
cin >> input; // get input from user
// output it in reverse order
for (int i = input.length(); i >= 0; --i) {
// output the char one by one according to character's index ( i-th letter in the word )
cout << input[i] ;
}
return 0;
}
I am missing an endl somewhere, I think. Because if I enter the word party the display shows as: ytrapPress any key to continue.....
Your code looks ok, just be aware that cin stops after the first whitespace so it might be better to use getline.
To have the wait message on a separate line put cout << "\n\n" before the return statement in main.
without the -1, it is probably safe here, as it will likely access the null terminator. But it is an error which in other circumstances could give undefined behaviour- that is, it could cause incorrect results, program crash etc.