Is string a keyword?
A string is NOT a keyword. It is one of the names of data types declared in the standard library.
How to check if a string is a keyword in C++?
- In C++ there is no special method to check whether the given string is a keyword or not. But there are a set of keywords present which is listed below-
“auto”, “break”, “case”, “char”, “const”, “continue”, “default”, “do”, “double”, “else”, “enum”, “extern”, “float”, “for”, “goto”, “if”, “int”, “long”, “register”, “return”, “short”, “signed”, “sizeof”, “static”, “struct”, “switch”, “typedef”, “union”, “unsigned”, “void”, “volatile”, “while”
- So, we need to check whether our search word is present in the list or not to determine whether it is a keyword or not. As there is no keyword “string” in the above-mentioned list, a string is not a keyword.
How to check if a string is a keyword in Java?
- Java provides a method iskeyword() which is used to find whether the given string is a keyword or not. It is present in javax.lang.model.SourceVersion package. It will return a boolean value either True or False.
Syntax:
SourceVersion.isKeyword(“word_to_check”)
Parameters:
This method accepts a string to check whether it is a keyword or not.
Returns:
Boolean values – True or False.
How to check if a string is a keyword in Python?
- Python provides a method iskeyword() which is used to find out whether the given string is a keyword or not. It is present in the keyword built-in module. So, before using this method we need to import the keyword module.
- The iskeyword() method accepts a word/string as a parameter and returns a Boolean value True or False based on the input argument. It returns true for all the reserved words and for the rest it will give the result as False.
Syntax:
keyword.iskeyword(“word_to_check”)
Below is the code for implementing the methods to check if “string” is a keyword or not.
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
set<string> keywords = { "alignas" ,
"alignof" ,
"and" ,
"and" ,
"and_eq" ,
"asm" ,
"atomic_cancel" ,
"atomic_commit" ,
"atomic_noexcept" ,
"auto" ,
"bitand" ,
"bitor" ,
"bool" ,
"break" ,
"case" ,
"catch" ,
"char" ,
"char16_t" ,
"char32_t" ,
"class" ,
"compl" ,
"concept" ,
"const" ,
"constexpr" ,
"const_cast" ,
"continue" ,
"co_await" ,
"co_return" ,
"co_yield" ,
"decltype" ,
"default" ,
"delete" ,
"do" ,
"double" ,
"dynamic_cast" ,
"else" ,
"enum" ,
"explicit" ,
"export" ,
"extern" ,
"false" ,
"float" ,
"for" ,
"friend" ,
"goto" ,
"if" ,
"import" ,
"inline" ,
"int" ,
"long" ,
"module" ,
"mutable" ,
"namespace" ,
"new" ,
"noexcept" ,
"not" ,
"not_eq" ,
"nullptr" ,
"operator" ,
"or" ,
"or_eq" ,
"private" ,
"protected" ,
"public" ,
"register" ,
"reinterpret_cast" ,
"requires" ,
"return" ,
"short" ,
"signed" ,
"sizeof" ,
"static" ,
"static_assert" ,
"static_cast" ,
"struct" ,
"switch" ,
"synchronized" ,
"template" ,
"this" ,
"thread_local" ,
"throw" ,
"true" ,
"try" ,
"typedef" ,
"typeid" ,
"typename" ,
"union" ,
"unsigned" ,
"using" ,
"virtual" ,
"void" ,
"volatile" ,
"wchar_t" ,
"while" ,
"xor" ,
"xor_eq" };
// Function to check c++ keyword bool is_keyword(string s) { return keywords.count(s); }
int main()
{ // Passing String to iskeyword() to check
// if it is a keyword or not
cout << is_keyword( "string" );
return 0;
} // This code is contributed by rakeshsahni
|
// Import necessary packages import java.io.*;
import javax.lang.model.SourceVersion;
class GFG {
public static void main(String[] args)
{
// Checking whether string is a keyword or not
System.out.println(
SourceVersion.isKeyword( "string" ));
}
} |
# Import necessary packages import keyword
if __name__ = = '__main__' :
# Passing String to iskeyword() to check
# if it is a keyword or not
print (keyword.iskeyword( 'String' ))
|
using System;
using System.Collections.Generic;
class Program
{ // HashSet to store C++ keywords
static HashSet< string > keywords = new HashSet< string >
{
"alignas" , "alignof" , "and" , "and_eq" , "asm" , "atomic_cancel" , "atomic_commit" , "atomic_noexcept" , "auto" ,
"bitand" , "bitor" , "bool" , "break" , "case" , "catch" , "char" , "char16_t" , "char32_t" , "class" , "compl" ,
"concept" , "const" , "constexpr" , "const_cast" , "continue" , "co_await" , "co_return" , "co_yield" , "decltype" ,
"default" , "delete" , "do" , "double" , "dynamic_cast" , "else" , "enum" , "explicit" , "export" , "extern" , "false" ,
"float" , "for" , "friend" , "goto" , "if" , "import" , "inline" , "int" , "long" , "module" , "mutable" , "namespace" ,
"new" , "noexcept" , "not" , "not_eq" , "nullptr" , "operator" , "or" , "or_eq" , "private" , "protected" , "public" ,
"register" , "reinterpret_cast" , "requires" , "return" , "short" , "signed" , "sizeof" , "static" , "static_assert" ,
"static_cast" , "struct" , "switch" , "synchronized" , "template" , "this" , "thread_local" , "throw" , "true" , "try" ,
"typedef" , "typeid" , "typename" , "union" , "unsigned" , "using" , "virtual" , "void" , "volatile" , "wchar_t" , "while" ,
"xor" , "xor_eq"
};
// Function to check if a word is a C++ keyword
static bool IsKeyword( string s) => keywords.Contains(s);
static void Main()
{
// Passing a string to IsKeyword() to check if it is a keyword or not
Console.WriteLine(IsKeyword( "string" ));
// Note: In C#, Console.WriteLine automatically adds a new line after the printed text.
}
} |
// A function to check if a given word is a JavaScript keyword or not const isKeyword = (word) => { // An array containing all the JavaScript keywords
const keywords = [
"abstract" , "await" , "boolean" , "break" , "byte" ,
"case" , "catch" , "char" , "class" , "const" , "continue" ,
"debugger" , "default" , "delete" , "do" , "double" ,
"else" , "enum" , "export" , "extends" , "false" , "final" ,
"finally" , "float" , "for" , "function" , "goto" , "if" ,
"implements" , "import" , "in" , "instanceof" , "int" ,
"interface" , "let" , "long" , "native" , "new" , "null" ,
"package" , "private" , "protected" , "public" , "return" ,
"short" , "static" , "super" , "switch" , "synchronized" ,
"this" , "throw" , "throws" , "transient" , "true" , "try" ,
"typeof" , "var" , "void" , "volatile" , "while" , "with" ,
"yield"
];
// Check if the word is in the keywords array
return keywords.includes(word);
}; // Calling the function with a test word and storing the result let ans = isKeyword( 'String' );
// Printing the result console.log(ans); // Contributed by adityae15 |
false