99. Check if the String Numeric or Alphabetic

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>
#include <stdint.h>

void classify_string(const char *str) {
    bool isNumeric=1;// this can be zero as be are doing reverse.
    bool isAlphabetic=1;

    for(int i=0; str[i]!='\0';i++){
        if(!(str[i] >='0' && str[i]<='9'))
            isNumeric = 0;
        
        if(!((str[i]>=65 && str[i]<=90) || (str[i]>=97 && str[i]<=122)))
            isAlphabetic = 0;
    }

    if(isNumeric)
        printf("NUMERIC");
    else if(isAlphabetic)
        printf("ALPHABETIC");
    else
        printf("MIXED");
}

int main() {
    char str[101];
    fgets(str, sizeof(str), stdin);

    // Remove newline
    uint8_t i = 0;
    while (str[i]) {
        if (str[i] == '\n') {
            str[i] = '\0';
            break;
        }
        i++;
    }

    classify_string(str);
    return 0;
}


/*

Interesting logic

while(*str != '\0')
    {
        if(*str >= '0' && *str<='9')
        {
            num=1;
        }
        else if((*str >= 'a' && *str <='z') || (*str >= 'A' && *str <='Z'))
        {
            alpha=1;
        }
        else
        {
            mixed=1;
        }
        str++;
    }
    if(mixed || (num && alpha))
    {
        printf("MIXED");
    }
    else if(num)
    {
        printf("NUMERIC");

    }
    else{printf("ALPHABETIC");}
}

*/

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote