fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5. char keywords[32][10] =
  6. {
  7. "auto","break","case","char","const","continue","default",
  8. "do","double","else","enum","extern","float","for","goto",
  9. "if","int","long","register","return","short","signed",
  10. "sizeof","static","struct","switch","typedef","union",
  11. "unsigned","void","volatile","while"
  12. };
  13.  
  14. const char *operators[] = {"+", "-", "*", "/", "=", "==", "!=", ">", "<", ">=", "<=", "&&", "||", "++", "--"};
  15. const char *separators[] = {",", ";", "(", ")", "{", "}", "[", "]"};
  16.  
  17. int isKeyword(char buffer[])
  18. {
  19. for(int i = 0; i < 32; ++i)
  20. {
  21. if(strcmp(keywords[i], buffer) == 0)
  22. {
  23. return 1;
  24. }
  25. }
  26. return 0;
  27. }
  28.  
  29. int isOperator(char ch)
  30. {
  31. for(int i = 0; i < sizeof(operators) / sizeof(operators[0]); ++i)
  32. {
  33. if(ch == operators[i][0])
  34. {
  35. if(strlen(operators[i]) > 1)
  36. {
  37. return 2;
  38. }
  39. return 1;
  40. }
  41. }
  42. return 0;
  43. }
  44.  
  45. int isSeparator(char ch)
  46. {
  47. for(int i = 0; i < sizeof(separators) / sizeof(separators[0]); ++i)
  48. {
  49. if(ch == separators[i][0])
  50. {
  51. return 1;
  52. }
  53. }
  54. return 0;
  55. }
  56.  
  57. int main()
  58. {
  59. char ch, buffer[15];
  60. FILE *fp;
  61. int j = 0,token=0;
  62.  
  63. fp = fopen("Firoz.txt", "r");
  64. if(fp == NULL)
  65. {
  66. printf("Error opening the file\n");
  67. exit(0);
  68. }
  69.  
  70. while((ch = fgetc(fp)) != EOF)
  71. {
  72. if(isalnum(ch))
  73. {
  74. buffer[j++] = ch;
  75. }
  76. else
  77. {
  78. if(j != 0)
  79. {
  80. buffer[j] = '\0';
  81. j = 0;
  82. token++;
  83. if(isKeyword(buffer))
  84. {
  85. printf("%s is a keyword\n", buffer);
  86. }
  87. else
  88. {
  89. if(buffer[0]-'0'<= 48 || buffer[0]-'0' >= 57)
  90. {
  91. printf("%s is an not identifier\n", buffer);
  92. }
  93. else
  94. {
  95. printf("%s is an identifier\n", buffer);
  96. }
  97. }
  98. }
  99.  
  100. if(isOperator(ch)==1)
  101. {
  102. token++;
  103. printf("%c is an operator\n", ch);
  104. if(isOperator(ch) == 2)
  105. {
  106. printf("Multi-character operator detected\n");
  107. }
  108. }
  109.  
  110. if(isSeparator(ch))
  111. {
  112. token++;
  113. printf("%c is a separator\n", ch);
  114. }
  115. }
  116. }
  117. printf("%d",token);
  118. fclose(fp);
  119. return 0;
  120. }
  121.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Error opening the file