Bug in The Spy Life C++ | SoloLearn: Learn to code for FREE!

+2

Bug in The Spy Life C++

Hello, I have a problem with this challenge. In test cases I got the right answer (for example: Expected answer: tacos Your Output: tacos) But it doesn’t want to accept it. I have done this challenge in python and java and I have not encountered this problem before. Is it a bug in the system or have I done something wrong? This is my code: #include <iostream> int main(){ using namespace std; string alph = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz "; string p; string d; getline(cin, p); for (int i = 0; i < p.length(); i++){ if (alph.find(p[i]) == p.npos){ p[i] = (char)0; } } for (int i = 0; i <= p.length(); i++){ d = d + p[p.length() - i]; } cout << d; }

5/5/2020 9:31:53 AM

Szymon

19 Answers

New Answer

+3

But you know that the index starts at 0 and the last index is length() - 1? ;)

+2

Your code is not working. It does not remove all non letters. I don't think that npos is helpful here. Btw: Why don't you just print p in reversed order with the condition that p[i] is inside alpha? pseudo code: for i = length - 1; i >= 0; i-- : if p[i] in alpha : print p[i] else continue

+2

Szymon I found this about npos: http://www.cplusplus.com/reference/string/string/npos/ I am wondering if you really know what npos is. I don't understand it and would be interested in how you get the idea to use npos.

+2

Okay, now I got it. In the reference about string.find() it is mentioned that it returns npos if no matches were found. Thanks.

+2

Try to revisit your code there most be a mistake that is causing it not to run properly.

+1

I think your second loop is wrong. i = 0 -> p.length() - 0 = p.length() p[p.length()] out of bounds -> should be garbage value. In java you would get an exception.

+1

The problem in not there, because when I tried another challenge with similar code (first loop) -Symbols- there is the same problem (or bug). In Symbols Challenge you just have to eliminate everything except letters, numbers and space, without reversing it (and the second loop is for reversing)

+1

Yea, I see that and I have changed it but it didn’t work. I have the same output as expected answer and still wrong 🤷🏼‍♂️

+1

Szymon I have my doubts about this: p[i] = (char) 0. The char is not visible. But did you check p.length() after the first loop? Not visible is not the same as not there. I would say there is still a char. How does your java code looks like? Can't you convert your java code to c++?

+1

Yea, you were right with a not visible char. Length did not change after first loop. I have changed it to just erase it and the length now changes. But... Sololearn still gives me 0/5 on cases. Answer is the same as expected, the length is correct...

+1

Nothing's wrong with this system... just change the last code to "cout << d.length()" to find the mistake I'll give you a hint: Your code has made a mess of whitespace (I don't know c++.. it's just my idea)

0

Szymon Can you please show me your new code?

0

https://code.sololearn.com/cz3nn6WDWr8o/#cpp

0

I’m doing my code on computer and then sending here. On VSC everything is working. The code is kinda working when I add (int) to p.length() (working on 2/5 cases) I have already figured this out with inalpha but I’m just wondering why it isn’t working here, but is working on my computer

0

So I have figured npos that way: When I wanted to find some element in a string (for example ‘x’) and it wasn’t there, I would get some crazy long number. It turnes out that when I cout << std::string::npos; I got the same number. So npos is like a value of element not found. So: string.find(‘x’) == std::string::npos string doesn’t contain ‘x’ string.find(‘x’) != std::string::npos string contains ‘x’

0

Value of npos is not important. It’s just the “representation” of not found

0

Szymon And erase? Maybe you did something there. But you deleted your code. So I can't test it.

0

any one can help me?

-1

Samsung 8