Check If a Given String is a Keyword in C



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.

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 volatile
do if static while

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

asm dynamic_cast namespace reinterpret_cast
bool explicit new static_cast
catch false operator template
class friend private this
const_cast inline public throw
delete mutable protected true
try typeid typename using
using using wchar_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
Updated on: 2019-10-07T07:28:38+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements