#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
char keywords[32][10] =
{
    "auto","break","case","char","const","continue","default",
    "do","double","else","enum","extern","float","for","goto",
    "if","int","long","register","return","short","signed",
    "sizeof","static","struct","switch","typedef","union",
    "unsigned","void","volatile","while"
};

const char *operators[] = {"+", "-", "*", "/", "=", "==", "!=", ">", "<", ">=", "<=", "&&", "||", "++", "--"};
const char *separators[] = {",", ";", "(", ")", "{", "}", "[", "]"};

int isKeyword(char buffer[])
{
    for(int i = 0; i < 32; ++i)
    {
        if(strcmp(keywords[i], buffer) == 0)
        {
            return 1;
        }
    }
    return 0;
}

int isOperator(char ch)
{
    for(int i = 0; i < sizeof(operators) / sizeof(operators[0]); ++i)
    {
        if(ch == operators[i][0])
        {
            if(strlen(operators[i]) > 1)
            {
                return 2;
            }
            return 1;
        }
    }
    return 0;
}

int isSeparator(char ch)
{
    for(int i = 0; i < sizeof(separators) / sizeof(separators[0]); ++i)
    {
        if(ch == separators[i][0])
        {
            return 1;
        }
    }
    return 0;
}

int main()
{
    char ch, buffer[15];
    FILE *fp;
    int j = 0,token=0;

    fp = fopen("Firoz.txt", "r");
    if(fp == NULL)
    {
        printf("Error opening the file\n");
        exit(0);
    }

    while((ch = fgetc(fp)) != EOF)
    {
        if(isalnum(ch))
        {
            buffer[j++] = ch;
        }
        else
        {
            if(j != 0)
            {
                buffer[j] = '\0';
                j = 0;
                token++;
                if(isKeyword(buffer))
                {
                    printf("%s is a keyword\n", buffer);
                }
                else
                {
                    if(buffer[0]-'0'<= 48 || buffer[0]-'0' >= 57)
                    {
                        printf("%s is an not identifier\n", buffer);
                    }
                    else
                    {
                        printf("%s is an identifier\n", buffer);
                    }
                }
            }

            if(isOperator(ch)==1)
            {
                token++;
                printf("%c is an operator\n", ch);
                if(isOperator(ch) == 2)
                {
                    printf("Multi-character operator detected\n");
                }
            }

            if(isSeparator(ch))
            {
                token++;
                printf("%c is a separator\n", ch);
            }
        }
    }
    printf("%d",token);
    fclose(fp);
    return 0;
}
