99. Check if the String Numeric or Alphabetic

Back To All Submissions
Previous Submission
Next Submission

Code

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

void classify_string(const char *str) {
    // Your logic here
    int is_alphabetic = 0, is_numeric = 0, mix = 0, i = 0;
    if (*(str + i) == '\0') {
        printf("MIXED");
        return;
    }
    while (*(str + i) != '\0') {
        char ch = *(str + i);
        if (ch >= '0' && ch <= '9') {
            is_numeric = 1;
        }
        else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
            is_alphabetic = 1;
        }
        else {
            mix = 1;
        }
        i++;
    }
    if (mix == 1) {
        printf("MIXED");
    }
    else if (is_numeric == 1 && is_alphabetic == 0) {
        printf("NUMERIC");
    }
    else if (is_alphabetic == 1 && is_numeric == 0) {
        printf("ALPHABETIC");
    }
    else {
        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

 

 

 

Was this helpful?
Upvote
Downvote