0% found this document useful (0 votes)
7 views

A1

Uploaded by

sephtis2004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

A1

Uploaded by

sephtis2004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

N Senapati

SE22UCSE246

#include <stdio.h>

#include <ctype.h>

#include <string.h>

// List of C keywords

const char *keywords[] = {

"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"

};

int isKeyword(const char *token) {

for (int i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {

if (strcmp(token, keywords[i]) == 0) {

return 1;

return 0;

int isConstant(const char *token) {

int i = 0;

if (isdigit(token[i]) || (token[i] == '-' && isdigit(token[i + 1]))) {


for (i = 1; i < strlen(token); i++) {

if (!isdigit(token[i])) {

return 0;

return 1;

return 0;

int isIdentifier(const char *token) {

if (isalpha(token[0]) || token[0] == '_') {

for (int i = 1; i < strlen(token); i++) {

if (!isalnum(token[i]) && token[i] != '_') {

return 0;

return 1;

return 0;

int isOperator(const char *token) {

const char *operators[] = {

"+", "-", "*", "/", "%", "++", "--", "=", "+=", "-=", "==",

"!=", ">", "<", ">=", "<=", "&&", "||", "!", "&", "|", "^", "~", "<<", ">>"

};

for (int i = 0; i < sizeof(operators) / sizeof(operators[0]); i++) {

if (strcmp(token, operators[i]) == 0) {
return 1;

return 0;

int main() {

char token[100];

printf("Enter a token: ");

scanf("%s", token);

if (isKeyword(token)) {

printf("'%s' is a keyword.\n", token);

} else if (isConstant(token)) {

printf("'%s' is a constant.\n", token);

} else if (isIdentifier(token)) {

printf("'%s' is an identifier.\n", token);

} else if (isOperator(token)) {

printf("'%s' is an operator.\n", token);

} else {

printf("'%s' is not recognized as a keyword, identifier, constant, or operator.\n", token);

return 0;

You might also like