Here's my question:
Vehicles are restricted for activities held in a place:
Enter a line of five characters to represent the license plate number.
The number can be 0 to 9 digits or uppercase letters, and at least one digit.
Output "illegal input" in the following cases:
I tested a few examples and they all passed, but the program didn't work in some cases that I didn't know about, and I wanted to know why.
Could you give a counter example or point out the error of my program?
Thank you very much.
And I'm using VS2022 as my editor and Windows 10 as my operating system
Below is an example of my test success and my code:
Example 1:
input: AbC13<enter key>
output: illegal input
Example 2:
input: AB12C<enter key>
output: Pass
Example 3:
input: 11111<enter key>
output: traffic limit
Example 4:
input: 22222<enter key>
output: Pass
Example 5:
input: QQQQQ<enter key>
output: illegal input
Example 6:
input: AAAA1<enter key>
output: traffic limit
Example 7:
input: BBBB2<enter key>
output: Pass
Example 8:
input: FFQ1A<enter key>
output: Pass
Example 9:
input: QQTS5S<enter key>
output: illegal input
Example 10:
input: 5aA<enter key>
output: illegal input
Example 11:
input:.wqe123<enter key>
output: illegal input
Example 12:
input:,-=-+<enter key>
output: illegal input
Example 13:
input:0123456789<enter key>
output: illegal input
#include <stdio.h>
int main()
{
char lic[10] = { 0 };
//lic Array used to store the input character
int flag = 0, i = 0, j, num = 0, odd = 0;
//flag Used to record whether a number appears
//There is a number when flag = 1
//num Used to record the number of times no other characters have appeared
//odd Used to record whether the last occurrence of the number is an odd number
//The last number is odd when odd = 1
for (i = 0; i < 10; i++)
{
scanf("%c", &lic[i]);
if (lic[i] == '\n')
{
break;//When the input "\n" exit the cycle
}
}
if (lic[5] != 0 && lic[5] != 10)//ASCII "10" on behalf of the carriage return, that is, '\ n'
{
flag = 0;
}
else
{
for (j = 0; j < 5; j++)
{
if (lic[j] <= '9' && lic[j] >= '0')
{
flag = 1;
num++;
}
if (lic[j] <= 'Z' && lic[j] >= 'A')
{
num++;
}
}
if ((lic[4] <= '9' && lic[4] >= '0') && (lic[4] % 2))
{
odd = 1;
}
}
if (num == 5 && flag == 1)
{
(odd) ? (printf("traffic limit\n")) : (printf("Pass\n"));
}
else
{
printf("illegal input\n");
}
return 0;
}
Thanks for all the comments, I have found the mistake, I have a problem understanding the topic, as mentioned in the comment.
I modified my program for this purpose and now it works properly.
#include <stdio.h>
int main()
{
char lic[10] = { 0 };
//lic Array used to store the input character
int flag = 0, i = 0, j, num = 0, odd = 0,temp = 0;
//flag Used to record whether a number appears
//There is a number when flag = 1
//num Used to record the number of times no other characters have appeared
//odd Used to record whether the last occurrence of the number is an odd number
//The last number is odd when odd = 1
gets(lic);
if (lic[5] != 0&&lic[5]!=10)//ASCII "10" on behalf of the carriage return, that is, '\ n'
{
flag = 0;
}
else
{
for (j = 0; j < 5; j++)
{
if (lic[j] <= '9' && lic[j] >= '0')
{
flag = 1;
temp = lic[j];
num++;
}
if (lic[j] <= 'Z' && lic[j] >= 'A')
{
num++;
}
}
if ((temp <= '9' && temp >= '0') && (temp % 2))
{
odd = 1;
}
}
if (num == 5&& flag == 1)
{
(odd) ? (printf("traffic limit\n")) : (printf("Pass\n"));
}
else
{
printf("illegal input\n");
}
return 0;
}