On the Extra Terrestrial problem on the code coach, my code output was the same as expected output still i failed the test cases | Sololearn: Learn to code for FREE!
+ 2

On the Extra Terrestrial problem on the code coach, my code output was the same as expected output still i failed the test cases

#include <iostream> using namespace std; int main() { string word; string word_arr[20]; int length; cin >> word; length = word.length(); for(int i = length; i >= 0; i--){ cout << word[i]; } return 0; } //please can someone tell me what went wrong? //I'm meant to reproduce a given word in reverse //thanks

2nd Jan 2020, 10:42 PM
Oluwatowo Rosanwo
Oluwatowo Rosanwo - avatar
3 Answers
+ 6
Since a string works similar to an array, its last character is at the position length() - 1 and not length() which you should keep in mind when initializing the running variable.
2nd Jan 2020, 10:47 PM
Shadow
Shadow - avatar
+ 1
Oooh.... .Thanks Ive done it... 😅😅
2nd Jan 2020, 10:50 PM
Oluwatowo Rosanwo
Oluwatowo Rosanwo - avatar
+ 1
Can you tell me why the array size is 20?
28th Apr 2020, 10:49 PM
Othman Haj salem
Othman Haj salem - avatar