count valid identifiers

#include <stdio.h>


#include <string.h>


int main()


{


    char filename[20];


    char str[100];


    printf("Enter the file name: ");


    scanf("%s", filename);


    FILE *fp = fopen(filename, "r");


    int count = 0;


    while (fscanf(fp, "%s", str) == 1)


    {


        int flag = 0, i;


        if ((str[0] >= 'a' && str[0] <= 'z') ||


            (str[0] >= 'A' && str[0] <= 'Z') ||


            (str[0] == '_'))


        {


            for (i = 1; i < strlen(str); i++)


            {


                if ((str[i] >= 'a' && str[i] <= 'z') ||


                    (str[i] >= 'A' && str[i] <= 'Z') ||


                    (str[i] >= '0' && str[i] <= '9') ||


                    (str[i] == '_'))


                {


                    flag = 0;

                }


                else


                {


                    flag = 1;


                    break;

                }

            }

        }


        else


        {


            flag = 1;

        }


        if (flag == 0)


        {


            count++;

        }

    }


    printf("Number of valid identifiers: %d", count);

}

Comments