0

Why this code is not getting valid for 10 ?

#include <stdio.h> #include <string.h> #include <ctype.h> int main() { char s[50]; int n; fgets(s,50,stdin); n=strlen(s); for (int i=0;i<n;i++) { if(isdigit(s[i])&&(isdigit(s[i+1])!='0')) { switch(s[i]) { case '1': printf("one");break; case '2': printf("two");break; case '3': printf("three"); break; case '4': printf("four");break; case '5': printf("five");break; case '6': printf("six");break; case '7': printf("seven");break; case '8': printf("eight");break; case '9': printf("nine");break; // case '10':printf("ten");break; default: printf("zero");break;}} else if((isdigit(s[i])=='1')&&(isdigit(s[i+1])=='0')) { printf("ten"); } else { printf ("%c",s[i]); } } return 0; }

16th Aug 2020, 10:11 AM
Abhinav Gourav Simhadri
Abhinav Gourav Simhadri - avatar
1 Odpowiedź
+ 3
Because you are often comparing a character to the return value of isdigit() (which is either zero or a positive number), in a context where you only want to compare the characters. For example, to check if the next character is a zero, s[ i + 1 ] == '0' would be sufficient. Also, keep in mind that ten is the only numeral with two digits. What happens if you increment once like you would normally do after printing "ten"?
16th Aug 2020, 10:34 AM
Shadow
Shadow - avatar