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
One programming language has the following keywords that cannot be used as identifiers

Solution

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

11 comments on “TCS Coding Question 4 | One programming language has the following keywords that …”


  • Gyanendra

    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");

    }
    }


  • Abhishek

    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.”);
    }

    }
    }


  • Book

    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();
    }
    }


  • Debasmita

    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


  • mekala

    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


  • Jatin

    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") ;

    }
    }


  • Arun

    #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”);
    }


  • MCE-EEE-

    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");
    }}


  • Yuvraj

    import keyword
    print(“”.join([“Its a keyword” if input() in keyword.kwlist else “No dude its not a keyword”]))


  • subhojit

    # 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))


  • Satya Sai Srija

    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’)