All submissions

Check if the String Numeric or Alphabetic

Code

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

void classify_string(const char *str) {
    // Your logic here
    int numeric=0, alphabetic=0, other=0;
    char *ptr;
    ptr = (char *) str;
    while (*ptr != '\0') {
        if ((*ptr >= '0')  && (*ptr <= '9')) {
            numeric++;
        }
        else if (((*ptr >= 'a') && (*ptr <= 'z')) ||
            ((*ptr >= 'A') && (*ptr <= 'Z')) ) {
                alphabetic++;
            }
        else {
            other++;
        }
        ptr++;
    }
    if ((alphabetic + numeric + other) == 0) {
        printf("VOID");
    }
    
    if ((alphabetic == 0) && (other == 0)) {
        printf("NUMERIC");
    }

    if ((numeric == 0) && (other == 0)) {
        printf("ALPHABETIC");
    }

    if (((numeric > 0) && (alphabetic >0)) || (other >0)) {
        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

count the occurences of digits, letters and other chars.

print statement as counts indicate.

 

 

Loading...

Input

123456

Expected Output

NUMERIC