I've written code for the Spy Life Code Coach. It works perfectly but the test cases come back wrong even though outputs match | SoloLearn: Learn to code for FREE!

0

I've written code for the Spy Life Code Coach. It works perfectly but the test cases come back wrong even though outputs match

#include <iostream> using namespace std; string words,ans; int main() { getline(cin,words); int len=words.length(); for(int i=0;i<len;i++) { if (isalpha(words[i]) || words[i]==' ') { ans+=words[i]; } } //cout<<ans; int lans=ans.length(); string output=""; for (int i=0; i<lans+1; i++) {output+=ans[lans-i];} cout<<output; return 0; } *Why does it not work?*

5/26/2020 10:06:26 AM

Olivia

4 Answers

New Answer

+2

Re check your code, you think it is correct but it is not. The for loop should be for (int i=0; i<lans; i++) { output+=ans[lans-i-1]; } Trace it out manually on paper. You will know the reason.

+2

I have tried your code in code coach (after changes) and it works fine for me. recheck the exit condition for "for" loop is i<lans not i<lans+1

+1

Ah thanks I forgot the lans--;

0

I tried the -1. No difference. I can see my output is identical to expected output. For some reason it's still staying red though and not computing as correct...