+ 5

Why test case 7 of password validation is not passing?

I have written a code for password validation that was asked in community challenge. The test case 7 is not passing , don't know what is wrong. Please help me with the code. I have provided my code here for you. https://code.sololearn.com/cHS9FN47kgmP/?ref=app

4th Nov 2020, 3:11 AM
rajshekhar y dodamani
rajshekhar y dodamani - avatar
4 Answers
+ 3
Please add #include <stdlib.h> if you want to use atoi() The problem is that when ch is not a number, atoi(&ch) returns 0. So special characters pass both the test for being a number and that for being one of the special characters. You can fix the problem by modifing the if in line 18: if(atoi(&ch)>0 && atoi(&ch)<=9 || ch == 48){...} Taking off "=" from ">=0" and adding a condition to allow zeros (stored in char as 48) to be counted as numbers. Or if you want you can just use isdigit() instead of atoi(). It checks wether the character is a number. Using isdigit() the condition becomes: if( isdigit( ch ) ) {...} If you want to use isdigit() add <ctype.h> instead of <stdlib.h> ps: you don't need both variables "c" and "ch", use just one of them to do everything.
4th Nov 2020, 11:23 AM
Davide
Davide - avatar
+ 2
Thank you Davide sir, I got the answer from you. The test case 7 was not passing because, The atoi function was giving the same result for this two cases: "0#" 1. For 0 it returned 0 and, 2. For # also it returned 0 Because of this even if the string didn't contained digits the program was printing strong if other conditions were met. So, to solve this issue instead of if(atoi(&ch)>=0 && atoi(&ch)<=9) we should use if(atoi(&ch)>0 && atoi(&ch)<=9 || ch==48). ch==48 is for checking 0.
4th Nov 2020, 2:01 PM
rajshekhar y dodamani
rajshekhar y dodamani - avatar
+ 2
rajshekhar y dodamani yes exactly😌 I am glad it worked!
4th Nov 2020, 2:13 PM
Davide
Davide - avatar
10th Oct 2022, 2:40 AM
Neeraj Kumar
Neeraj Kumar - avatar