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

void tokenize(const char *input)
{
    int i = 0;
    int len = strlen(input);

    printf("\nTokens Found:\n");

    while (i < len)
    {
        // Skip whitespace
        if (isspace(input[i]))
        {
            i++;
            continue;
        }

        // Check for two-character operators
        if (i + 1 < len)
        {
            char two[3] = {input[i], input[i + 1], '\0'};

            if (strcmp(two, "+=") == 0 || strcmp(two, "-=") == 0 ||
                strcmp(two, "*=") == 0 || strcmp(two, "/=") == 0 ||
                strcmp(two, ">=") == 0 || strcmp(two, "<=") == 0 ||
                strcmp(two, "==") == 0 || strcmp(two, "!=") == 0)
            {

                printf("OPERATOR: %s\n", two);
                i += 2;
                continue;
            }
        }

        // Single-character operators
        if (input[i] == '+' || input[i] == '-' || input[i] == '*' ||
            input[i] == '/' || input[i] == '=' || input[i] == '>' ||
            input[i] == '<' || input[i] == '!')
        {

            printf("OPERATOR: %c\n", input[i]);
            i++;
            continue;
        }

        // Identifiers / keywords
        if (isalpha(input[i]) || input[i] == '_')
        {
            char buffer[256];
            int j = 0;
            while (i < len && (isalnum(input[i]) || input[i] == '_'))
            {
                buffer[j++] = input[i++];
            }
            buffer[j] = '\0';
            printf("IDENTIFIER: %s\n", buffer);
            continue;
        }

        // Numbers
        if (isdigit(input[i]))
        {
            char buffer[256];
            int j = 0;
            while (i < len && isdigit(input[i]))
            {
                buffer[j++] = input[i++];
            }
            buffer[j] = '\0';
            printf("NUMBER: %s\n", buffer);
            continue;
        }

        // Unknown character
        printf("UNKNOWN: %c\n", input[i]);
        i++;
    }
}

int main()
{
    char input[1024];
    printf("Enter a simple C code snippet: ");
    fgets(input, sizeof(input), stdin);

    // Remove trailing newline
    input[strcspn(input, "\n")] = '\0';

    tokenize(input);
    return 0;
}