TCS NQT Set 1 | Day 1 2020 | keyword or not

TCS NQT Set 1 | Day 1 2020 | keyword or not

TCS NQT Set 1 | Day 1 2020 | keyword or not

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

Logic:

  • Get the input string from the user
  • Then compare all given keywords like break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type and var using strcmp() function
  • Then print the output whether the keyword is found or not

TCS NQT Set 1:

Program:

#include<stdio.h>
#include<string.h>
void main()
{
    char a[30];
    scanf("%s",&a);   
    //compare the sting with all keywords
    if((strcmp(a,"defer")==0)||(strcmp(a,"if")==0)||(strcmp(a,"for")==0)||(strcmp(a,"return")==0)||(strcmp(a,"break")==0)(strcmp(a,"case")==0)||(strcmp(a,"continue")==0)||(strcmp(a,"default")==0)||(strcmp(a,"else")==0)||(strcmp(a,"func")==0)||(strcmp(a,"goto")==0)||(strcmp(a,"map")==0)||(strcmp(a,"range")==0)||(strcmp(a,"struct")==0)||(strcmp(a,"type")==0)||(strcmp(a,"var")==0))
    {
        printf("%s is a keyword",a);
    }
    else                    //print when keyword not matching
    {
        printf("%s is not a keyword",a);
    }
}

You might also  like:

Set 2 | Day 1 2020

Set 3 | Day 1 2020

Set 4 | Day 1 2020

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.
0 0 votes
Article Rating
Subscribe
Notify of
guest
5.4K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
5.4K
0
Would love your thoughts, please comment.x
()
x