All submissions

Check if the String Numeric or Alphabetic

Code

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

void classify_string(const char *str) {
    bool numeric = false; //To indicate if the string has numeric
    bool alphabetic = false; //To indicate if the string has the alphabets
    bool mixed = false; //To indicate if the string has symbols

    //Traverse through the array
    while(*str != '\0'){
        // Check for digit condition
        if((*str >= 48) && (*str <= 57)) numeric = true;
        // Check for alphabetic condition
        else if(((*str >= 65) && (*str <= 90))||((*str >= 97) && (*str <= 122))) alphabetic = true;
        //Check for other symbols condition
        else{
            mixed = true;
            //If the string is mixed it cannot be numeric or alphabetic at same time
            numeric = false;
            alphabetic = false;
        }

        //Increment the pointer to traverse the array
        *str++;
    }
    //Additionally we should add the condition if the string is numeric and as well as alphabetical, then also it is considered as mixed
    if(numeric && alphabetic){
        mixed = true;
        //If the string is mixed it cannot be numeric or alphabetic at same time
        numeric = false;
        alphabetic = false;
    }
    if(numeric) printf("NUMERIC");
    else if(alphabetic) 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

 

 

 

Loading...

Input

123456

Expected Output

NUMERIC