0

Extra terrestrial answer test failing but my output is exactly the same

#include <iostream> using namespace std; int main() { string word; cin >>word; string reverse; for(int i=word.length();i>=0;i--){ reverse+=word[i]; } cout << reverse; return 0; }

10th Apr 2022, 4:42 PM
BENtO
BENtO - avatar
3 ответов
+ 1
Start with int i = word.length() -1
10th Apr 2022, 4:50 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Thanks, it worked.. But why?
10th Apr 2022, 5:04 PM
BENtO
BENtO - avatar
+ 1
Indexes starts from 0 , not 1 Ex: if word = "abcde"; It return word.length() as 5, but maximum valid index is 4 only. and 5 is out of range Valid indexes are 0 to length-1 only..
10th Apr 2022, 5:09 PM
Jayakrishna 🇮🇳