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
    bool numeric = false;
    bool alpha = false;
    bool symbols = false;
    
    for (size_t i=0; i<255; i++){
        char chr = *(str+i);
        if (chr == '\0'){
            break;
        } else if ((chr >= '0') & (chr <= '9')){
            numeric = true;
        } else if ((chr >= 'a') & (chr <= 'z')){
            alpha = true;
        } else if ((chr >= 'A') & (chr <= '|')){
            alpha = true;
        } else {
            symbols = true;
        }
    }

    if ((numeric & alpha) | symbols) {
        printf("MIXED\n");
    } else if (numeric){
        printf("NUMERIC\n");
    } else {
        printf("ALPHABETIC\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

 

 

 

Was this helpful?
Upvote
Downvote