Check if the String Numeric or Alphabetic

Code

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

int is_digit(char c){
    return (c >= '0' && c <= '9');
}

int is_alpha(char c){
    return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}

int is_symbol(char c){
    return (!is_digit(c) && !is_alpha(c));
}


void classify_string(const char *str) {
    int i =0;
    int count = 0; //get count of string
    int count_alp =0;
    int count_digit =0;
    //get string character number
    while(str[i] != '\0'){
        count++;
        i++;
    }

    for(i =0; i< count;i++){
        if(is_digit(str[i])){
            count_digit++;
        }else if(is_alpha(str[i])){
            count_alp++;
        }
    }

    if(count_digit == count){
        printf("NUMERIC");
    }else if(count_alp == count){
        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

 

 

 

Upvote
Downvote
Loading...

Input

123456

Expected Output

NUMERIC