0

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

3
  • 2
    Remember that strings in C are really called null-terminated strings. That termination is what makes a string a string. That means a string with two characters needs to have an array of three characters, to fit the terminator. Now think about it in the context of your str array. Commented May 27, 2017 at 17:34
  • Try some test cases of your own. Commented May 27, 2017 at 18:48
  • You show it passing 5 of 6 tests, but only show us 3 inputs. Can you add the other three inputs? Commented May 27, 2017 at 18:49

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This was the failing test case.
0

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.

1 Comment

Thanks for the points. I am relatively new to coding. Some of these mistakes were made in effort to try and find which test case I was missing.
-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");

}

1 Comment

Explain why he can use that use and there's no try, you can or not can

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.