Check if the String Numeric or Alphabetic

Code

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

void classify_string(const char *str) {

    int i=0;
    int j=0;

    while(str[i] !='\0')
    {
        if((str[i] >= '0') &&  (str[i] <='9'))
        {
            i++;
        }
        else
        { 
            break;
        }  
       
    }
   // printf("%d\n",i);
    if(i== strlen(str))
    {
        printf("NUMERIC");
    }
    while(str[j] !='\0')
    {
        if(str[j] >= 'a' &&  str[j] <='z' || str[j] >= 'A' &&  str[j] <='Z' )
        j++;
        else break;     
        
    }
    if(j== strlen(str))
    {
        printf("ALPHABETIC");
    }
    if( !(i==strlen(str) || j == strlen(str)) )
    printf("MIXED");

    // Your logic here
}

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