Home > Backend Development > C++ > C program to check if a given string is a keyword?

C program to check if a given string is a keyword?

王林
Release: 2023-09-08 14:45:04
forward
1080 people have browsed it

C program to check if a given string is a keyword?

Keywords are words that are predefined or reserved in the C library, have a fixed meaning, and are used to perform internal operations. The C language supports more than 64 keywords.

Each keyword exists in lowercase letters, such as auto, break, case, const, continue, int, etc.

The 32 keywords in C language can also be used in 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

##This is in C The newly added 30 reserved words are not in C language.

asmdynamic_castnamespacereinterpret_castboolexplicitnewstatic_cast##catchclassconst_castdeletetryusing
false operator template
friend private this
inline public throw
mutable protected true
typeid typename using
using wchar_t

Input: str=”for”
Output: for is a keyword
Copy after login

Explanation

    Keywords are reserved words that cannot be used as variable names in the program.
  • There are 32 keywords in the C programming language.
  • Compares the string with each keyword, if the strings are the same, then the string is a keyword.

Example

Example

#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);
}
Copy after login

Output

which is a keyword
Copy after login

The above is the detailed content of C program to check if a given string is a keyword?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template