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


Keyword is a predefined or reserved word which is available in C++ library with a fixed meaning and used to perform an internal operation. C++ Language supports more than 64 keywords.

Every Keyword exists in lower case letters like auto, break, case, const, continue, int etc.

32 Keywords in C++ Language which is also available in the C language.

autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultgotosizeofvolatile
doifstaticwhile

These are 30 reserved words that were not in C, but added to C++

asmdynamic_castnamespacereinterpret_cast
boolexplicitnewstatic_cast
catchfalseoperatortemplate
classfriendprivatethis
const_castinlinepublicthrow
deletemutableprotectedtrue
trytypeidtypenameusing
usingusingwchar_t


Input: str=”for”
Output: for is a keyword

Explanation

  • Keywords are reserved words which cannot be used as variable names in program.

  • There are 32 keywords in the C programming language.

Compare the string with each keyword if the string is same then string is keyword

Example

 Live Demo

#include <stdio.h>
#include <string.h>
int main() {
   char keyword[32][10]={
      "auto","double","int","struct","break","else","long",
      "switch","case","enum","register","typedef","char",
      "extern","return","union","const","float","short",
      "unsigned","continue","for","signed","void","default",
      "goto","sizeof","voltile","do","if","static","while"
   } ;
   char str[]="which";
   int flag=0,i;
   for(i = 0; i < 32; i++) {
      if(strcmp(str,keyword[i])==0) {
         flag=1;
      }
   }
   if(flag==1)
      printf("%s is a keyword",str);
   else
      printf("%s is not a keyword",str);
}

Output

which is a keyword
raja
Published on 07-Oct-2019 07:26:51
Advertisements