Word is Key – TCS NQT Question 3

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
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
bool isKeyword(char* str)
{
if (!strcmp(str, "break")
|| !strcmp(str, "case")
||!strcmp(str, "continue")
|| !strcmp(str, "default")
|| !strcmp(str, "defer")
|| !strcmp(str, "else")
|| !strcmp(str, "for")
|| !strcmp(str, "func")
|| !strcmp(str, "goto")
|| !strcmp(str, "if")
|| !strcmp(str, "map")
|| !strcmp(str, "range")
|| !strcmp(str, "return")
|| !strcmp(str, "struct")
|| !strcmp(str, "type")
                || !strcmp(str, "var"))
return (true);
return (false);
}
int main()
{
    char a[20];
    scanf("%s",a);
isKeyword(a) ? printf("Yesn"): printf("Non");
return 0;
} 
Previous Article

Tree Shadow within Matrix

Next Article

Money with Baba

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *