All submissions

Check if the String Numeric or Alphabetic

Code

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

void classify_string(const char *str) {
    // Your logic here
    bool isNumeric = false;
    bool isAlpha = false;
    if(str[0] == '\0')
    {
        return;
    }

    if(isalpha(str[0]))
    {
        isAlpha = true;
    }
    else if(isdigit(str[0]))
    {
        isNumeric = true;
    }
    else
    {
        printf("MIXED\n");
        return;
    }


    int i = 0;
    if(isAlpha)
    {
        while(str[i] != '\0')
        {
            if(!isalpha(str[i]))
            {
                printf("MIXED\n");
                return;
            }
            i++;
        }
        printf("ALPHABETIC\n");

    }
    else
    {
        while(str[i] != '\0')
        {
            if(!isdigit(str[i]))
            {
                printf("MIXED\n");
                return;
            }
            i++;
        }
        printf("NUMERIC\n");

    }





}

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