fork download
  1. /******** Name :- Md Firoz Islam ***********
  2. University :- Northern University Bangladesh
  3. ********** Dhaka,Bangladesh ***************/
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. #define ll long long
  7. #define yes cout<<"YES"<<endl;
  8. #define no cout<<"NO"<<endl;
  9. #define faster ios::sync_with_stdio(false),cin.tie(NULL);
  10.  
  11. const char* keywords[32] = {
  12. "auto","break","case","char","const","continue","default",
  13. "do","double","else","enum","extern","float","for","goto",
  14. "if","int","long","register","return","short","signed",
  15. "sizeof","static","struct","switch","typedef","union",
  16. "unsigned","void","volatile","while"
  17. };
  18.  
  19. const char *operators[] = {"+", "-", "*", "/", "%", "=", "==", "!=", ">", "<", ">=", "<=", "&&", "||", "++", "--"};
  20. const char *separators[] = {",", ";", "(", ")", "{", "}", "[", "]"};
  21.  
  22. int isKeyword(const char buffer[]) {
  23. for(int i = 0; i < 32; ++i) {
  24. if(strcmp(keywords[i], buffer) == 0) {
  25. return 1;
  26. }
  27. }
  28. return 0;
  29. }
  30.  
  31. int isOperator(char ch) {
  32. for(int i = 0; i < sizeof(operators) / sizeof(operators[0]); ++i) {
  33. if(ch == operators[i][0]) {
  34. if(strlen(operators[i]) > 1) {
  35. return 2;
  36. }
  37. return 1;
  38. }
  39. }
  40. return 0;
  41. }
  42.  
  43. int isSeparator(char ch) {
  44. for(int i = 0; i < sizeof(separators) / sizeof(separators[0]); ++i) {
  45. if(ch == separators[i][0]) {
  46. return 1;
  47. }
  48. }
  49. return 0;
  50. }
  51.  
  52. int main() {
  53. char ch, buffer[15];
  54. ifstream fp("Firoz.txt");
  55. int j = 0;
  56.  
  57. if(!fp.is_open()) {
  58. cout << "Error opening the file" << endl;
  59. return 1;
  60. }
  61.  
  62. while(fp.get(ch)) {
  63. if(isalnum(ch)) {
  64. buffer[j++] = ch;
  65. }
  66. else {
  67. if(j != 0) {
  68. buffer[j] = '\0';
  69. j = 0;
  70.  
  71. if(isKeyword(buffer)) {
  72. cout << buffer << " is a keyword" << endl;
  73. } else {
  74. cout << buffer << " is an identifier" <<endl;
  75. }
  76. }
  77.  
  78. if(isOperator(ch) == 1) {
  79. cout << ch << " is an operator" << endl;
  80. if(isOperator(ch) == 2) {
  81. cout << "Multi-character operator detected" << endl;
  82. }
  83. }
  84.  
  85. if(isSeparator(ch)) {
  86. cout << ch << " is a separator" << endl;
  87. }
  88. }
  89. }
  90.  
  91. fp.close();
  92. return 0;
  93. }
  94.  
Success #stdin #stdout 0.03s 25632KB
stdin
Standard input is empty
stdout
/******** Name :- Md Firoz Islam ***********
University :- Northern University Bangladesh
********** Dhaka,Bangladesh ***************/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define faster ios::sync_with_stdio(false),cin.tie(NULL);

const char* keywords[32] = {
    "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(const 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];
    ifstream fp("Firoz.txt");
    int j = 0;

    if(!fp.is_open()) {
        cout << "Error opening the file" << endl;
        return 1;
    }

    while(fp.get(ch)) {
        if(isalnum(ch)) {
            buffer[j++] = ch;
        }
        else {
            if(j != 0) {
                buffer[j] = '\0';
                j = 0;

                if(isKeyword(buffer)) {
                    cout << buffer << " is a keyword" << endl;
                } else {
                    cout << buffer << " is an identifier" <<endl;
                }
            }

            if(isOperator(ch) == 1) {
                cout << ch << " is an operator" << endl;
                if(isOperator(ch) == 2) {
                    cout << "Multi-character operator detected" << endl;
                }
            }

            if(isSeparator(ch)) {
                cout << ch << " is a separator" << endl;
            }
        }
    }

    fp.close();
    return 0;
}