99. Check if the String Numeric or Alphabetic

Back To All Submissions
Previous Submission
Next Submission

Code

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

#define NUM     0x01
#define ALP     0x02
#define SIGN    0x04

void classify_string(const char *str) {
    // Your logic here
    if (!str || str[0] == '\0') return;

    uint8_t flag = 0x000;
    while (*str != '\0') {
        char c = *str;
        if ('0' <= c && c <= '9')
            flag |= NUM;
        else if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
            flag |= ALP;
        else {
            flag |= SIGN;
        }
        if (((flag & (NUM | ALP)) == (NUM | ALP)) || ((flag & SIGN) == SIGN)) {
            printf("MIXED");
            return;
        }
        
        str++;
    }

    if (flag & NUM)
        printf("NUMERIC");
    else
        printf("ALPHABETIC");

}

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;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote