Aritra Dasgupta
Aritra Dasgupta

Reputation: 49

Cannot identify error with code, failing a test case

I have to submit this code as a solution. My code runs perfectly for the given test cases, but I am not able to submit as the code fails one of the tests in the solver. Please help if you can. Any help is appreciated.

Gift Article with Digits

Many customers liked the gift articles with digits inscribed on them and they started buying them for gifting for Birthdays and anniversaries. One customer came to purchase a gift for his mom's 25th wedding anniversary and another customer came to purchase a gift for his son's 18th Birthday. They were disappointed to see only single digits inscribed on the gift items.

Seeing the craze for this kind of gift items, Nisha gave a bulk order for gift items with 2 digit numbers inscribed on them. The parcel arrived when she was busy and her 4 year old son started arranging the newly arrived items in the rack for display. But he has placed all items upside down. She needs to change the orientation of the items.

But to her surprise, she found that some 2-digit numbers were valid when read both ways. [Eg. 68 read upside down would be 89 which is also a valid number] Help Nisha in identifying all such 2 digit numbers.

TestCase

Input 1

18

Output 1

YES

Input 2

46

Output 2

NO

Input 3

a4

Output 3

Invalid Input

C code:

#include<stdio.h>
#include<ctype.h> 
#include<string.h>
int main()
{    

char str[2];

scanf("%s",str);

int flag=0;


if (strlen(str)!=2)
{
    flag=2;goto label;
}
else if (str[1]=='0')
{
    flag=1;goto label;
}

for(int i=0;i<2;i++)
{

    if(isdigit(str[i]))
    {
        if((str[i]!='0')&&(str[i]!='1')&&(str[i]!='6')&&(str[i]!='8')&&
    (str[i]!='9'))
        {
        flag=1;break;
        }

    }
    else 
    {flag=2;break;}

    }

label:
if (flag==0) printf("YES");
else if (flag==1) printf("NO");
else if (flag==2) printf("Invalid Input");
return 0;
}

The output after evaluation is as follows:

output

Upvotes: 0

Views: 479

Answers (3)

Roshni Patel
Roshni Patel

Reputation: 1

You can try the below code!

#include<stdio.h>
#include<string.h>
void main(){
char str[2];
int a,b;
scanf("%s",str);
a=str[0];
b=str[1];
if(strlen(str)!=2)
printf("Invalid Input");
else if(str[0]=='0')
printf("NO");
else if((a>47&&a<58)&&(b>47&&b<58))
{
    if(((str[0]=='1')||(str[0]=='6')||(str[0]=='8')||(str[0]=='9'))&&((str[1]=='1')||(str[1]=='6')||(str[1]=='8')||(str[1]=='9')))
    printf("YES");
    else
    printf("NO");
}
else
printf("Invalid Input");

}

Upvotes: -1

user3629249
user3629249

Reputation: 16540

the main problem with the code is the following two lines

char str[2];

scanf("%s",str);

When the scanf() input/conversion specifier is "%s" then the function will append a NUL ('\0') char to the input AND the input is not stopped until a white space character is encountered.

White space: space, tab, newline sequence

Therefore, when using the '%s" input/conversion specifier there are two considerations:

  1. the input buffer must be 1 char longer that the max allowed number of input characters
  2. the MAX_CHARACTERS modifier must be used, that is 1 less than the length of the input buffer.

Therefore, those two lines should be:

    char str[3];     // allows room for 2 characters plus NUL terminator

    scanf("%2s",str); // only allow user to input two characters

however, there are some other problems with the code.

This line:

    if (strlen(str)!=2)

does not allow for when there is only a single digit I.E. 1...9 inclusive.

it is a very poor programming practice to use the goto + label sequence. It invariable results in 'spaghetti' code.

this code block:

    else if (str[1]=='0')
    {
        flag=1;
        goto label;
    }

is not correct as it rejects 10, 20, 30, 40, 50, 60, 70, 80, 90. Note: in C, an array index is in the range 0...(one less than the number of entries in the array) Note: '0' is 0x30 in hex and the NUL terminator is 0x00 in hex.

This line:

    for( int i=0; i<2; i++ )

is making the assumption that all the 'ages' are 2 digit numbers. That excludes the ages 1...9 inclusive. Suggest:

    for( size_t i=0; i<=strlen(str); i++ )

Note: strlen() returns a size_t, not an int and returns the index to the NUL char

    flag = 2;  // initialize to indicate invalid input
    if( strlen( str ) )
    { // then some characters entered by user

        for( size_t i=0; i<strlen( str ); i++ )
        {
            ... // check for invertible digit 
            ... // check for non digit 
        }
    }

    switch( flag )
    {
        case 0:
            printf( "YES\n" );
            break;

        case 1:
            printf( "NO\n" );
            break;

        default:
            printf( "Invalid Input\n" );
            break;
    } // end switch

    // Note: on modern C compilers, 
    // when the returned value from 'main()' is always 0, 
    // then no 'return 0;' statement actually needed
    return 0;
} // end function: main

However, the above code snippet does not handle when the user input contains 1 invertible digit and 1 non invertible digit nor when any of the user input is not a digit. I'll let you supply the appropriate logic. The above should get you started in the right direction.

Upvotes: 0

Erich Kitzmueller
Erich Kitzmueller

Reputation: 36977

The program's output is incorrect e.g. for 4a, because you break out of the loop after checking the first digit.

The program's answer is NO when it should be Invalid Input.

Upvotes: 2

Related Questions