Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

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

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

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


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