All submissions

Check if the String Numeric or Alphabetic

Code

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

void classify_string(const char *str) {
    char *p = (char *)str;
    bool num = false;
    bool alpha = false;
    bool mix = false;
    
    if (!*p) {
        printf("MIXED\n");
        return;
    }
    
    while (*p) {
        if (*p >= '0' && *p <= '9')
            num = true;
        else if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z'))
            alpha = true;
        else
            mix = true;
        p++;
    }

    if (alpha && !num && !mix)
        printf("ALPHABETIC\n");
    else if (!alpha && num && !mix)
        printf("NUMERIC\n");
    else
        printf("MIXED\n");
}

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

 

 

 

Loading...

Input

123456

Expected Output

NUMERIC