TCS Coding Question 4 | One programming language has the following keywords that …
Problem Statement (Word is Key)
One programming language has the following keywords that cannot be used as identifiers:
break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var
Write a program to find if the given word is a keyword or not
Test cases
Case 1
- Input – defer
- Expected Output – defer is a keyword
Case 2
- Input – While
- Expected Output – while is not a keyword
Solution
C Program
C++
Java
Python
C Program
#include<stdio.h> #include<string.h> int main(){ char str[16][10] = {"break", "case", "continue", "default", "defer", "else","for", "func", "goto", "if", "map", "range", "return", "struct", "type", "var"}; char input[20]; int flag = 0; scanf("%s",input); for(int i = 0; i<16;i++){ if(strcmp(input,str[i]) == 0){ flag = 1; break; } } if(flag==1){ printf("%s is a keyword",input); } else{ printf("%s is not a keyword",input); } return 0; }
C++
#include<iostream> #include<string.h>
using namespace std;
int main(){ char str[16][10] = {"break", "case", "continue", "default", "defer", "else","for", "func", "goto", "if", "map", "range", "return", "struct", "type", "var"}; char input[20]; int flag = 0; cin >> input; for(int i = 0; i<16;i++){ if(strcmp(input,str[i]) == 0){ flag = 1; break; } } if(flag==1){ cout << input << " is a keyword"; } else{ cout << input << " is not a keyword"; } return 0; }
Java
import java.util.Scanner; public class Main { public static void main(String args[]) { String str[]= {"break", "case", "continue", "default", "defer", "else","for", "func", "goto", "if", "map", "range", "return", "struct", "type", "var"}; int flag = 0; Scanner sc = new Scanner(System.in); String input=sc.nextLine(); for(int i = 0; i<16;i++){ if(str[i].equals(input)){ flag = 1; break; } } if(flag==1){ System.out.println(input+" is a keyword"); } else{ System.out.println(input+" is not a keyword"); } } } //The above is code is contributed by SOUMYA CHAKRABORTY (PrepInsta Placement Cell Student)
Python
keyword = {"break", "case", "continue", "default", "defer", "else", "for",
"func", "goto", "if", "map", "range", "return", "struct", "type", "var"}
input_var = input()
if input_var in keyword:
print(input_var+ " is a keyword")
else:
print(input_var+ " is a not keyword")
#the above code was submitted by Prerana H (PrepInsta Placement Cell Student)
Alternate Solutions
Java
import java.util.*;
public class Main
{
public static void main(String[] args) {
String[] s=new String[]{"break","case","continue","default","defer","else","for","func","goto","if","map","range","return","struct","type","var"};
List<String> l=Arrays.asList(s);
Scanner sin=new Scanner(System.in);
String s1=sin.nextLine();
if(l.contains(s1))
System.out.println(s1+" is a keyword");
else
System.out.println(s1+" is not a keyword");
}
}
asd
Login/Signup to comment
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
String str=scn.nextLine();
String[] pstr={“break”,”case”, “continue”, “default”, “defer”, “else”, “for”, “func”, “goto”, “if”, “map”, “range”, “return”, “struct”, “type”, “var”};
for(int i=0;i<pstr.length;i++){
String check=pstr[i];
if(str.equals(check)){
System.out.println(str+" is a keyword");
return;
}
}
System.out.println(str+" is not Keyword");
}
}
import java.util.*;
public class Main{
public static void main(String agrs[])
{
ArrayList str = new ArrayList();
str.add(“break”);
str.add(“case”);
str.add(“contiune”);
str.add(“default”);
str.add(“defer”);
str.add(“else”);
str.add(“for”);
str.add(“func”);
str.add(“goto”);
str.add(“if”);
str.add(“map”);
str.add(“range”);
str.add(“return”);
str.add(“struct”);
str.add(“type”);
str.add(“var”);
int flag =0;
Scanner scn = new Scanner(System.in);
String input = scn.nextLine();
ListIterator listIterator
= str.listIterator();
while(listIterator.hasNext())
{
String temp = listIterator.next();
if(temp.equals(input))
{
flag =1;
break;
}
}
if(flag==1)
{
System.out.println(input + “is a keyword.”);
}else {
System.out.println(input + “is not keyword.”);
}
}
}
import java.util.*;
public class QuestionFour {
public static void main(String args[]){
Scanner reader = new Scanner(System.in);
HashSet keyWords= new HashSet();
String str[]= {“break”, “case”, “continue”, “default”, “defer”, “else”,”for”, “func”, “goto”,
“if”, “map”, “range”, “return”, “struct”, “type”, “var”};
for(String a:str){
keyWords.add(a);
}
String check = reader.nextLine();
if(keyWords.contains(check)){
System.out.println(“Keyword”);
}else{
System.out.println(“not a keyword”);
}
reader.close();
}
}
import java.util.*;
class Keyword {
public static void main(String args[]){
HashMap input = new HashMap ();
input.put(“break”,”Keyword”);
input.put(“case”,”Keyword”);
input.put(“continue”,”Keyword”);
input.put(“else”,”dKeywor”);
input.put(“default”,”Keyword”);
input.put(“defer”,”Keyword”);
input.put(“for”,”Keyword”);
input.put(“func”,”Keyword”);
input.put(“goto”,”Keyword”);
input.put(“if”,”Keyword”);
input.put(“map”,”Keyword”);
input.put(“range”,”Keyword”);
input.put(“return”,”Keyword”);
input.put(“struct”,”Keyword”);
input.put(“type”,”Keyword”);
input.put(“var”,”Keyword”);
String str = new Scanner(System.in).nextLine();
if((input.get(str)) == “Keyword”)
{
System.out.println(“Keyword”);
}
else
System.out.println(“Not a Keyword”);
}
}
//Code in java Using Hashmap
my python code
l1=[“break”,”case”,”continue”,”default”,”defer”,”else”,”for”,”func”,”goto”,”if”,”map”,”range”,”return”,”struct”,”type”,”var”]
word=input()
if word in l1:
print(word,”is a keyword”)
else:
pass
public class Main {
public static void main(String args[]) {
String str = “var” ;
String[] str1 = new String[]{“break”, “case”, “continue”, “default”, “defer”, “else”, “for”, “func”, “goto”, “if”, “map”, “range”, “return”, “struct”, “type”, “var”};
for(int i = 0 ;i<str1.length ;i++){
if(str1[i] == str){
System.out.print(str + " " + "is a keyword") ;
return ;
}
}
System.out.print(str + " " + "is not a keyword") ;
}
}
#java string matches example
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String text = scan.nextLine();
String val[] = {“break”, “case”, “continue”, “default”, “defer”, “else”,
“for”, “func”, “goto”, “if”, “map”, “range”, “return”, “struct”, “type”, “var”};
for(String s : val){
if(text.matches(s)){
System.out.println(text+” is a keyword”);
return;}
}
System.out.println(text+” is not a keyword”);
}
import java.util.*;
class Main{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int flog=0;
String input=sc.nextLine();
String a[]={“break”, “case”, “continue”, “default”, “defer”, “else”,”for”, “func”, “goto”,
“if”, “map”, “range”, “return”, “struct”, “type”, “var”};
for(int i=0;i<a.length;i++){
if(a[i].equals(a)){
flog=1;}
}System.out.println(flog==1?a+" is a keyword":" is not a keyword");
}}
import keyword
print(“”.join([“Its a keyword” if input() in keyword.kwlist else “No dude its not a keyword”]))
# Simple Python Code
import keyword
k=input()
if k in keyword.kwlist:
print(“{} is a keyword”.format(k))
else:
print(“{} is not a keyword”.format(k))
lis=[‘break’, ‘case’, ‘continue’, ‘default’, ‘defer’, ‘else’, ‘for’, ‘func’, ‘goto’, ‘if’, ‘map’, ‘range’, ‘return’, ‘struct’, ‘type’, ‘var’]
key=input()
if key in lis:
print(key,”is a keyword”)
else:
print(key,’is not a keyword’)