Check if the String Numeric or Alphabetic

Code

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

int is_num(char s) {
    if (s >= '0' && s <= '9') {
        return 1;
    } else {
        return 0;
    }
}

int is_alph(char s) {
    if ((s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z')) {
        return 1;
    } else {
        return 0;
    }
}

int (*arr[2])(char s) = {&is_num,&is_alph};

void classify_string(const char *str) {
    // Your logic here
    uint8_t val = 0;
    if (is_num(*str)) {
        val = 0;
    } else if (is_alph(*str)) {
        val = 1;
    } else {
        printf("MIXED");
        return;
    }

    str++;

    while (*str != '\0') {
        if (!arr[val](*str)) {
            printf("MIXED");
            return;
        }
        str++;
    }

    if (val == 0) {
        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

 

 

 

Upvote
Downvote
Loading...

Input

123456

Expected Output

NUMERIC