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.
- Keywords are reserved words which cannot be used as variable names.
- There are 32 keywords in C programming language.
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() { isKeyword("geeks") ? printf("Yes\n") : printf("No\n"); isKeyword("for") ? printf("Yes\n") : printf("No\n"); return 0; } |
chevron_right
filter_none
Output:
No Yes
Recommended Posts:
- C program to print a string without any quote (singe or double) in the program
- Use of explicit keyword in C++
- restrict keyword in C
- _Generic keyword in C
- C++ mutable keyword
- Check if a string is palindrome in C using pointers
- C++ | Static Keyword | Question 1
- C++ | Static Keyword | Question 2
- C++ | Static Keyword | Question 3
- C++ | friend keyword | Question 1
- C++ | friend keyword | Question 2
- C++ | const keyword | Question 5
- C++ | Static Keyword | Question 6
- C++ | const keyword | Question 5
- C++ | Static Keyword | Question 5
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



