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 length = 0;



    int has_alpha = 0;

    int has_num = 0;

    int has_mix = 0;



    while(*str != '\0'){

        char c = *str;

        if(c >= '0' && c <= '9'){

            has_num = 1;

        }

        else if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){

            has_alpha = 1;

        }

        else{

            has_mix = 1;

        }

        str++;

    }

    if(has_mix || (has_alpha && has_num)){

        printf("MIXED");

    }  

    else if(has_num){

        printf("NUMERIC");

    }

    else if(has_alpha){

        printf("ALPHABETIC");

    }

}



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