inhuman_Arif

1 no

Dec 9th, 2022
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. void keyword(char str[10]) {
  5.   if (strcmp("for", str) == 0 || strcmp("while", str) == 0 ||
  6.       strcmp("do", str) == 0 || strcmp("int", str) == 0 ||
  7.       strcmp("float", str) == 0 || strcmp("char", str) == 0 ||
  8.       strcmp("double", str) == 0 || strcmp("static", str) == 0 ||
  9.       strcmp("switch", str) == 0 || strcmp("case", str) == 0)
  10.     printf("\n%s is a keyword", str);
  11.   else
  12.     printf("\n%s is an identifier", str);
  13. }
  14.  
  15. int main() {
  16.       FILE *f1, *f2, *f3;
  17.       char c, str[10], st1[10];
  18.       int num[100], lineno = 0, tokenvalue = 0, i = 0, j = 0, k = 0;
  19.       f1 = fopen("input", "r");
  20.       f2 = fopen("identifier", "w");
  21.       f3 = fopen("specialchar", "w");
  22.       while ((c = getc(f1)) != EOF) {
  23.         if (isdigit(c)) {
  24.           tokenvalue = c - '0';
  25.           c = getc(f1);
  26.           while (isdigit(c)) {
  27.             tokenvalue *= 10 + c - '0';
  28.             c = getc(f1);
  29.           }
  30.           num[i++] = tokenvalue;
  31.           ungetc(c, f1);
  32.         } else if (isalpha(c)) {
  33.           putc(c, f2);
  34.           c = getc(f1);
  35.           while (isdigit(c) || isalpha(c) || c == '_' || c == '$') {
  36.             putc(c, f2);
  37.             c = getc(f1);
  38.           }
  39.           putc(' ', f2);
  40.           ungetc(c, f1);
  41.         } else if (c == ' ' || c == '\t')
  42.           printf(" ");
  43.         else if (c == '\n')
  44.           lineno++;
  45.         else
  46.           putc(c, f3);
  47.       }
  48.       fclose(f2);
  49.       fclose(f3);
  50.       fclose(f1);
  51.       printf("\nThe numbers in the program are ");
  52.       for (j = 0; j < i; j++)
  53.         printf("%d ", num[j]);
  54.       printf("\n");
  55.       f2 = fopen("identifier", "r");
  56.       k = 0;
  57.       printf("The keywords and identifiersare: ");
  58.       while ((c = getc(f2)) != EOF) {
  59.         if (c != ' ')
  60.           str[k++] = c;
  61.         else {
  62.           str[k] = '\0';
  63.           keyword(str);
  64.           k = 0;
  65.         }
  66.       }
  67.       fclose(f2);
  68.       f3 = fopen("specialchar", "r");
  69.       printf("\nSpecial characters are ");
  70.       while ((c = getc(f3)) != EOF)
  71.       printf("%c ", c);
  72.       printf("\n");
  73.       fclose(f3);
  74.       printf("Total number of lines are:%d", lineno);
  75.       return 0;
  76. }
  77.  
  78. /*
  79. Input
  80. #include <stdio.h>
  81. int main() {    
  82.  
  83.     int number1, number2, sum;
  84.    
  85.     printf("Enter two integers: ");
  86.     scanf("%d %d", &number1, &number2);
  87.  
  88.     // calculating sum
  89.     sum = number1 + number2;      
  90.    
  91.     printf("%d + %d = %d", number1, number2, sum);
  92.     return 0;
  93. }
  94. */
Advertisement
Add Comment
Please, Sign In to add comment