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) {
    bool isDigit = true;
    bool isAlpha = true;
    bool isOther = true;

    while(*str != '\0'){
        if((*str >= '0') && (*str <= '9')){
            isAlpha = false;
        }
        else if(((*str >= 'A') && (*str <= 'Z')) || ((*str >= 'a') && (*str <= 'z'))){
            isDigit = false;
        }
        else{
            isOther = false;
        }
        str++;
    }
    if(isAlpha & !isDigit & isOther){
        printf("ALPHABETIC");
    }
    else if(!isAlpha & isDigit & isOther){
        printf("NUMERIC");
    }
    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