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 count = 0, numeric = 0, alpha = 0;

    while(str[count] != '\0'){
            
        if(str[count]>='A' && str[count]<='Z' || str[count]>='a' && str[count]<='z')
            alpha++;
        else if(str[count]>= '0' && str[count]<= '9')
            numeric++;
        else{
            printf("MIXED");
            return;
        }
        count++;
    }
    if(numeric != 0 && alpha != 0)
        printf("MIXED");
    else if(alpha != 0)
        printf("ALPHABETIC");
    else
        printf("NUMERIC");

}

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