Check if the String Numeric or Alphabetic

Code

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

void classify_string(const char *str) {
    // Your logic here
    uint8_t temp[3] = {0};
    int length = 0;
    while (str[length] != '\0') {
        if (str[length] >= '0' && str[length] <= '9') temp[0] = 1;
        else if ((str[length] >= 'a' && str[length] <= 'z')||(str[length] >= 'A' && str[length] <= 'Z')) temp[1] = 1;
        else temp[2] = 1;
        length++;
    }
    if (temp[0] == 1 && temp[1] == 1) printf("MIXED\n");
    if (temp[0] == 1 && temp[1] == 0) printf("NUMERIC\n");
    if (temp[0] == 0 && temp[1] == 1 && temp[2] == 0) printf("ALPHABETIC\n");
    if (temp[0] == 0 && temp[1] == 0) printf("MIXED\n");
    if (temp[0] == 0 && temp[1] == 1 && temp[2] == 1) 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

 

 

 

Upvote
Downvote
Loading...

Input

123456

Expected Output

NUMERIC