-3

This is a code of checking a given string is an identifier or a keyword. Here is the code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>


int main(){

    int i = 0, flag = 0;
    char a[10][10] = {"int", "float", "break", "long", "char", "for", "if", "switch", "else", "while"}, string[10];

    //clrscr();

    printf("Enter a string :");
    gets(string);

    /*----Checking whether the string is in array a[][]----*/

    for(i = 0 ; i < 10; i++){
        if( (strcmp( a[i], string) == 0) )
            flag = 1;
    }

    /*----If it is in the array then it is a keyword----*/

    if( flag == 1)
        printf("\n%s is a keyword ", string);

    /*----Otherwise check whether the string is an identifier----*/
    else{
        flag = 0;
        /*----Checking the 1st character*----*/

        if( (string[0] == '_') || ( isalpha(string[0]) != 0 ) ){
            /*---Checking rest of the characters*---*/
            for(i = 1; string[i] != '\0'; i++)
            if( (isalnum(string[i]) == 0 ) && (string[i]!='_') )
                flag = 1;
        }
        else
            flag = 1;
        if( flag == 0)
            printf("\n%s is an identifier ", string);
        else
            printf("\n%s is neither a keyword nor an identifier ", string);
    }
        getch();
}
  • I want to do this code more easily. And is it possible to get or identified the all keywords without declaring in the char? And how to do that?

Can S.O provide me that code?

Raindrop7
  • 3,889
  • 3
  • 16
  • 27
Rashed Sami
  • 141
  • 1
  • 4
  • 12
  • 1
    If your code was more consistently formatted and indented it might be easier to follow the logic. – Galik Oct 08 '17 at 16:48
  • 1
    It would be a lot easier if you used [std::string](http://en.cppreference.com/w/cpp/string/basic_string) and [std::vector](http://en.cppreference.com/w/cpp/container/vector) rather than arrays and character arrays. – Galik Oct 08 '17 at 16:49
  • If you have working code to be improved, better ask at [SE Code Review](https://codereview.stackexchange.com/). – user0042 Oct 08 '17 at 16:49
  • how to do that easily by using std::string and std::vector ?? it would be great if you provide me the code @Galik – Rashed Sami Oct 08 '17 at 16:53
  • @RashedSami This isn't a tutorial writing site. You can google for tutorials or, better still, work from a good book: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Galik Oct 08 '17 at 16:54
  • What is this line: `char a[10][10] = {"int", "float", "break", "long", "char", "for", "if", "switch", "else", "while"}, string[10]; `? – Raindrop7 Oct 08 '17 at 17:56
  • Never use `gets`. – melpomene Oct 08 '17 at 18:03
  • Your list of keywords is very incomplete (e.g. `class` is missing). – melpomene Oct 08 '17 at 18:05
  • IMHO, the *easiest* method is to put your keywords (as `std::string`) into a `std::vector`, and use `std::find` to search the vector. You can use `cin >> ` to input the `std::string`. – Thomas Matthews Oct 08 '17 at 18:14

1 Answers1

1

Here is an easy method:

static const std::string keywords[] =
{
  "char", "class", 
  "struct",
  /* ... */
};
static const size_t keyword_quantity =
  sizeof(keywords) / sizeof(keywords[0]);

std::string search_word;
cin >> search_word;

std::string const * const iterator = 
    std::find(&keywords[0], &keywords[keyword_quantity],
              search_word);
if (iterator != &keywords[keyword_quantity])
{
  cout << "Word is a keyword!\n";
}

The std::string data type makes text or string handling easier.
The std::find function is easy so you don't have to write it (and it's already tested).

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154