Check if the String Numeric or Alphabetic

Code

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

void classify_string(const char *str) {

    int cnt1 = 0;
    int cnt2 = 0;
    int cnt3 = 0;
    // Your logic here
    while(*str != '\0'){

        if(*str >= '0' && *str <= '9') cnt1++;
        
        if((*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z')) cnt2++;

        if(!(*str >= 'a' && *str <= 'z') && !(*str >= 'A' && *str <= 'Z') && !(*str >= '0' && *str <= '9')) cnt3++;



        str++;

    }

    if (cnt1 > 0 && (cnt2 > 0 || cnt3 > 0)) {printf("%s","MIXED");}
    else if (cnt2 > 0 && (cnt1 > 0 || cnt3 > 0)) {printf("%s","MIXED");}
    else if(cnt1 > 0) {printf("%s","NUMERIC");}
    else if(cnt2 > 0) {printf("%s","ALPHABETIC");}
    else if(cnt1 > 0 && cnt3 > 0){printf("%s","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