fork download
  1. l#include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. void keyword(char str[])
  6. {
  7. const char *keys[] = {
  8. "for","while","do","int","float","char","double",
  9. "static","switch","case","return","if","else","break","continue"
  10. };
  11. int n = sizeof(keys)/sizeof(keys[0]);
  12.  
  13. for(int i=0;i<n;i++)
  14. if(strcmp(str,keys[i])==0) {
  15. printf("\n%s is a keyword",str);
  16. return;
  17. }
  18. printf("\n%s is an identifier",str);
  19. }
  20.  
  21. int main()
  22. {
  23. FILE *f1,*f2,*f3;
  24. char c, str[50];
  25. int num[100], lineno=1, tokenvalue=0;
  26. int i=0,j=0,k=0;
  27.  
  28. printf("Enter the C program (Ctrl+D to end):\n");
  29.  
  30. f1 = fopen("input.c","w");
  31. while((c=getchar())!=EOF)
  32. putc(c,f1);
  33. fclose(f1);
  34.  
  35. f1 = fopen("input.c","r");
  36. f2 = fopen("identifiers.txt","w");
  37. f3 = fopen("specialchar.txt","w");
  38.  
  39. while((c=getc(f1))!=EOF)
  40. {
  41. if(isdigit((unsigned char)c))
  42. {
  43. tokenvalue = c - '0';
  44. while(isdigit((unsigned char)(c=getc(f1))))
  45. tokenvalue = tokenvalue*10 + (c-'0');
  46. num[i++] = tokenvalue;
  47. ungetc(c,f1);
  48. }
  49. else if(isalpha((unsigned char)c) || c=='_')
  50. {
  51. putc(c,f2);
  52. while(isalnum((unsigned char)(c=getc(f1))) || c=='_')
  53. putc(c,f2);
  54. putc(' ',f2);
  55. ungetc(c,f1);
  56. }
  57. else if(c==' ' || c=='\t') { }
  58. else if(c=='\n') lineno++;
  59. else putc(c,f3);
  60. }
  61.  
  62. fclose(f1); fclose(f2); fclose(f3);
  63.  
  64. printf("\nNumbers found: ");
  65. for(j=0;j<i;j++)
  66. printf("%d ",num[j]);
  67.  
  68. f2=fopen("identifiers.txt","r");
  69. printf("\n\nKeywords and Identifiers:");
  70. k=0;
  71. while((c=getc(f2))!=EOF)
  72. {
  73. if(c!=' ')
  74. str[k++]=c;
  75. else {
  76. str[k]='\0';
  77. keyword(str);
  78. k=0;
  79. }
  80. }
  81. fclose(f2);
  82.  
  83. f3=fopen("specialchar.txt","r");
  84. printf("\n\nSpecial characters: ");
  85. while((c=getc(f3))!=EOF)
  86. printf("%c",c);
  87. fclose(f3);
  88.  
  89. printf("\n\nTotal number of lines: %d\n", lineno);
  90.  
  91. return 0;
  92. }
  93.  
Success #stdin #stdout #stderr 0.02s 6820KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/rG1d3r/prog:92:1: Syntax error: end_of_file_in_quasi_quotation
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit