Check if the String Numeric or Alphabetic

Code

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

void classify_string(const char *str) {
    // Your logic here
    int numeric = 0;
    int alphabetic = 0;
    int symbols = 0;

    while (*str != 0) {
        if (*str >= 'a' && *str <= 'z' || *str >= 'A' && *str <= 'Z') {
            alphabetic = 1;
        } else if (*str >= '0' && *str <= '9') {
            numeric = 1;
        } else {
            symbols = 1;
        }
        str++;
    }

    if (numeric == 1 && alphabetic == 1 
              || alphabetic == 1 && symbols == 1 
              || numeric == 1 && symbols ==1) {
        printf("MIXED");
    } else if (alphabetic == 1 && numeric == 0 && symbols == 0) {
        printf("ALPHABETIC");
    } else if (numeric == 1 && alphabetic == 0 && symbols == 0) {
        printf("NUMERIC");
    }
}

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