+ 1

Problem from code coach.

https://www.sololearn.com/coach/55?ref=app. my solution in C++ is below (it's not working) : #include<string> #include<iostream> using namespace std; bool is_letter(char a) { int b; b = (int)a;/*converts char to int(ASCII value)*/ if(b==32)/* 32 is the ASCII value of space */ { return true; } if((b>=65||b<=90)||(b>=97||b<=122)) /*letters 'A' to 'Z' & 'a' to 'z' respectively */ { return true; } return false; }; int main() { string message,hidden; getline(cin,message); int length = message.size(); int i,j=0; for(i=(length-1);i>=0;i--) { if(is_letter(message[i])) { hidden[j]=message[i]; j++; } } cout<<hidden<<endl; return 0; }

15th Jul 2025, 9:25 AM
Rowlet
Rowlet - avatar
8 Antworten
15th Jul 2025, 10:04 AM
Mila
Mila - avatar
+ 6
Rowlet , there are some issues in your code: the conditional statement is not correct, and no output is given. i recommend you to fix these issues step by step outside the code coach, so that debugging can be done. i did a try that is quite easy to read and to understand. for checking if the input characters are alphabetical or a space, we can use the functions: isalpha() and isspace() in an if... conditional. this runs inside a for loop that picks the characters one at a time. inside the if conditional the new string will be built. to reverse the newly created string, we can use the reverse() function. >> to open the file, do not click the pur text link but the other one. https://sololearn.com/compiler-playground/c39N5ZxZWEIy/?ref=app
15th Jul 2025, 11:30 AM
Lothar
Lothar - avatar
+ 2
Mila , the code you presented has a truncated output - the capital letters are missing. input of `d89%l++5r19o7W *o=l645le9H` => `ello orld` but should `Hello World`
15th Jul 2025, 11:37 AM
Lothar
Lothar - avatar
+ 2
Mila true. the problem only used lowercase. so your solution is passes all tests. Lothar yes, it's good to strive for robustness in our code, using isalpha handles both upper and lowercase. it is not specifically stated in the code that only lowercase is used, so we can't make that assumption. here is the code i used. reverse iterating removes the need for algorithm::reverse. directly displaying the characters save the step of collecting them to a variable. https://sololearn.com/compiler-playground/cOJrR9zuDLJC/?ref=app
15th Jul 2025, 2:11 PM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li , Mila , sorry to say, but the test case given in the task description of this code coach uses this string as input: "d89%l++5r19o7W *o=l645le9H" which contains >>2 upper case letters `H` , `W`<<. Sample Input: d89%l++5r19o7W *o=l645le9H Sample Output: Hello World so for me it is clear, that upper case has to be handled.
15th Jul 2025, 6:28 PM
Lothar
Lothar - avatar
+ 1
Lothar , For this particular task, The Spy Life only has a solution for lowercase letters. Try copying this and solving the task. All tests confirm this. If you really don't like it, simply add the uppercase letters to the "alphabet" array.
15th Jul 2025, 12:01 PM
Mila
Mila - avatar
+ 1
Bob_Li , Excellent! You have very short and clear code.
15th Jul 2025, 2:17 PM
Mila
Mila - avatar
0
Lothar , I updated my code. I hope it will suit you now. What was in the task text is not what was in the tests.
15th Jul 2025, 7:17 PM
Mila
Mila - avatar