Extra-terrestrials problem | SoloLearn: Learn to code for FREE!


Extra-terrestrials problem

Hi c++ developers, I'm solving Extra-terrestrials problem and I struggled with print the output with spaces in the for loop properly. Please explain to me how can I solve this(the problem is to print the input in the reverse order) #include <iostream> #include <cstring> using namespace std; int main() { char word[100]; cin.getline(word,100); for (int i=strlen(word); i>=0;i-- ){ cout<<word[i]; } return 0; }

3/3/2021 11:50:30 AM

Maryam Magdy

5 Answers

New Answer


Printing spaces is no issue, but you need to remember that arrays are null-indexed. This means you need to start iterating at strlen( word ) - 1 because otherwise you print the null character stored after the string as well, which messes up the comparison between your output and the solution.


Usually the null character has no graphical represantation on a terminal at all, which is why it can be confusing if your output seemingly matches the solution, but does not appear to be correct.


Thank you for your reply. So, the null character is a space?


Yes that's what confused me, but I get it. An Invisible place :D


Yor comments were helpful Thx. And I just earned 10XP yay