Check if the String Numeric or Alphabetic

Code

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

void classify_string(const char *str) {
    // Your logic here
    int ret = 0; // 0x01 : NUMERIC, 0x02 : ALPHABETIC, 0x03 : MIXED
    while (0 != *str) {
        if ('0' <= *str && *str <= '9') {
            ret |= 0x01;
        } else if (('a' <= *str && *str <= 'z') || ('A' <= *str && *str <= 'Z')) {
            ret |= 0x02;
        } else {
            ret |= 0x03;
        }
        str++;
    }
    if (0x01 == ret) {
        printf("NUMERIC");
    } else if (0x02 == ret) {
        printf("ALPHABETIC");
    } else if (0x03 == ret) {
        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;
}

Solving Approach

my logic is better than the solution

 

 

Upvote
Downvote
Loading...

Input

123456

Expected Output

NUMERIC