+ 1
Need help with Zip Code Validator on C from Code Coach
Something wrong with it, it passed every test case but one and I don’t know where bug is. Please help. https://code.sololearn.com/cgl8zAofnpe2/?ref=app
9 ответов
0
Tim There are certain things you need to know about C language .
C doesn't go for bound checking i.e. It doesn't care about the length of your array and the length of your input.
So even if you have an array of length 5 but your input length is 6 , it will read 6 chars regardless of the size of array .
So to make sure, your code passes
Do
len=strlen(code)
If len>5 || len<5
print false
+ 2
Hima it finally works. Thank you.
I knew the length was the case, but didn’t know how to manage that and make a proper length check.
+ 1
little much help you ❤️
https://code.sololearn.com/c5MSGIbnD3h4/?ref=app
0
The code can be shortened by just defining a valid range.
If(code[i]>=48 && code[i]<=57)
{
//do your thing
}
and are you sure about the size of your array?
0
Hima what 48 and 57 mean? It works, thanks, but I don’t understand how.
Postal code needs to be 5 numbers in length, and if you use bigger size, it will show a different result.
0
Tim whenever you compare something, if the data type of both the operands are not same , the one which has lower rank gets converted into the higher one .
Here, int>char , and int value of a char is its ascii value .
e.g. '0' is 48 , '1' is 49 and so on.
Also , you said length of the postal code should be 6 but you have just taken 5 .
change it to 6
0
Hima I got it, thanks
Length is actually 5 (just edited), I accidentaly missclicked.
But still something is missing to complete the challenge
0
Tim Could you show me the problem statement?
0
Hima
As headmaster of the post office, sometimes people write zip codes that don't exist or zip codes that are not valid.
You are tasked with making a system to check if the inputted zip code is a valid zip code.
Task:
Write a program that takes in a string representing a zip code. Output true or false if it is a valid zip code or not.
A valid zip code is only numbers, must be 5 characters in length, and contain no spaces.
Input Format:
A string containing a zip code.
Output Format:
A string: true is the input is a valid zip code, or false, if it is not.
Sample Input:
752f78
Sample Output:
false
Explanation:
A valid zip code contains 5 digits, and no letters or spaces. The input is 6 characters long and contains letters, making it an invalid zip code.