Open In App

C program to check if a given string is Keyword or not

Given a string, the task is to write a program that checks if the given string is a keyword or not.

Examples:



Input: str = "geeks"
Output: geeks is not a keyword
Input: str = "for"
Output: for is a keyword




// C program to check whether a given
// string is a keyword or not
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
 
// Function to check whether the given
// string is a keyword or not
// Returns 'true' if the string is a KEYWORD.
bool isKeyword(char* str)
{
    if (!strcmp(str, "auto") || !strcmp(str, "default")
        || !strcmp(str, "signed") || !strcmp(str, "enum")
        || !strcmp(str, "extern") || !strcmp(str, "for")
        || !strcmp(str, "register") || !strcmp(str, "if")
        || !strcmp(str, "else") || !strcmp(str, "int")
        || !strcmp(str, "while") || !strcmp(str, "do")
        || !strcmp(str, "break") || !strcmp(str, "continue")
        || !strcmp(str, "double") || !strcmp(str, "float")
        || !strcmp(str, "return") || !strcmp(str, "char")
        || !strcmp(str, "case") || !strcmp(str, "const")
        || !strcmp(str, "sizeof") || !strcmp(str, "long")
        || !strcmp(str, "short") || !strcmp(str, "typedef")
        || !strcmp(str, "switch")
        || !strcmp(str, "unsigned") || !strcmp(str, "void")
        || !strcmp(str, "static") || !strcmp(str, "struct")
        || !strcmp(str, "goto") || !strcmp(str, "union")
        || !strcmp(str, "volatile"))
        return (true);
    return (false);
}
 
// Driver code
int main()
{
    printf("Is \"Geeks\" Keyword: \t");
    isKeyword("geeks") ? printf("Yes\n") : printf("No\n");
    printf("Is \"for\" Keyword: \t");
    isKeyword("for") ? printf("Yes\n") : printf("No\n");
    return 0;
}

Output
Is "Geeks" Keyword:     No
Is "for" Keyword:     Yes

We can also define an array of strings that contains all the keywords of C language instead of using them in the conditional statement like if else. After that, we can use the loop to compare the string entered by the user to the keywords list. The below program implements this approach.






// C program to check whether a given
// string is a keyword or not
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
 
// Function to check whether the given string is a keyword
// or not Returns 'true' if the string is a KEYWORD.
bool isKeyword(const char* str)
{
    char keywords[32][10]
        = { "auto",   "default""signed",   "enum",
            "extern", "for",      "register", "if",
            "else",   "int",      "while",    "do",
            "break""continue", "double",   "float",
            "return", "char",     "case",     "const",
            "sizeof", "long",     "short",    "typedef",
            "switch", "unsigned", "void",     "static",
            "struct", "goto",     "union",    "volatile" };
 
    for (int i = 0; i < 32; ++i) {
        if (strcmp(str, keywords[i]) == 0) {
            return true;
        }
    }
    return false;
}
 
int main()
{
    char input[50];
    printf("Enter a string: ");
    scanf("%s", input);
 
    if (isKeyword(input)) {
        printf("Yes");
    }
    else {
        printf("No");
    }
 
    return 0;
}

Output

Enter a string: volatile
Yes

Article Tags :