Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program that will read a token from a user and find its type

Posted By: Easy Tutor     Category: C++ Programming     Views: 4506

A C++ Program that will read a token from a user and find its type.

Code for Program that will read a token from a user and find its type in C++ Programming


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


 typedef enum { false=0,true=1 } bool;

 /*************************************************************************//*************************************************************************///-----------------------  Function Prototypes  -------------------------///*************************************************************************//*************************************************************************/constbool isPositiveNumber(constchar*);
 constbool isNegativeNumber(constchar*);
 constbool isPositiveDigit(constchar*);
 constbool isNegativeDigit(constchar*);
 constbool isIdentifier(constchar*);
 constbool isKeyword(constchar*);

 /*************************************************************************//*************************************************************************///------------------------------  main( )  ------------------------------///*************************************************************************//*************************************************************************/void main( )
    {
       clrscr( );

       char Token[25]={NULL};

       cout<<"Enter a token : ";
       gets(Token);

       if(isKeyword(Token)==true)
      cout<<"The given Token is a Keyword.";

       elseif(isIdentifier(Token)==true)
      cout<<"The given Token is an Identifier.";

       elseif(isPositiveDigit(Token)==true)
      cout<<"The given Token is a Positive Digit.";

       elseif(isNegativeDigit(Token)==true)
      cout<<"The given Token is a Negative Digit.";

       elseif(isPositiveNumber(Token)==true)
      cout<<"The given Token is a Positive Number.";

       elseif(isNegativeNumber(Token)==true)
      cout<<"The given Token is a Negative Number.";

       else
      cout<<"Error : The given Token is invalid.";

       getch( );
    }

 /*************************************************************************//*************************************************************************///----------------------  Function Definitions  -------------------------///*************************************************************************//*************************************************************************//*************************************************************************///-----------------------  isPositiveNumber( )  -------------------------///*************************************************************************/constbool isPositiveNumber(constchar* Token)
 {
    if((!isdigit(Token[0]) && Token[0]!='+') || Token[0]=='0')
       returnfalse;

    if(Token[0]=='+' && (Token[1]=='0' || !isdigit(Token[1])))
       returnfalse;

    int length=strlen(Token);

    for(int count=1;count<length;count++)
    {
       if(!isdigit(Token[count]))
      returnfalse;
    }

    returntrue;
 }

 /*************************************************************************///-----------------------  isNegativeNumber( )  -------------------------///*************************************************************************/constbool isNegativeNumber(constchar* Token)
 {
    if(Token[0]!='-' || !isdigit(Token[1]) || Token[1]=='0')
       returnfalse;

    int length=strlen(Token);

    for(int count=2;count<length;count++)
    {
       if(!isdigit(Token[count]))
      returnfalse;
    }

    returntrue;
 }

 /*************************************************************************///------------------------  isPositiveDigit( )  -------------------------///*************************************************************************/constbool isPositiveDigit(constchar* Token)
 {
    if(strlen(Token)>2 || strlen(Token)==0)
       returnfalse;

    elseif(strlen(Token)==2 && (Token[0]!='+' ||
                       Token[1]=='0' || !isdigit(Token[1])))
       returnfalse;

    elseif(strlen(Token)==1 && (Token[0]=='0' || !isdigit(Token[0])))
       returnfalse;

    returntrue;
 }

 /*************************************************************************///------------------------  isNegativeDigit( )  -------------------------///*************************************************************************/constbool isNegativeDigit(constchar* Token)
 {
    if(strlen(Token)!=2 || Token[0]!='-' ||
                       Token[1]=='0' || !isdigit(Token[1]))
       returnfalse;

    returntrue;
 }

 /*************************************************************************///-------------------------  isIdentifier( )  ---------------------------///*************************************************************************/constbool isIdentifier(constchar* Token)
 {
    if(!isalpha(Token[0]) && Token[0]!='_')
       returnfalse;

    int length=strlen(Token);

    for(int count=1;count<length;count++)
    {
       if(!isalpha(Token[count]) && !isdigit(Token[count]) &&
                             Token[count]!='_')
      returnfalse;
    }

    returntrue;
 }

 /*************************************************************************///---------------------------  isKeyword( )  ----------------------------///*************************************************************************/constbool isKeyword(constchar* Token)
 {
    if(strlen(Token)>16 || strlen(Token)==0)
       returnfalse;

    constchar Keywords[64][20]={
                  "asm","auto","bool","break","case","catch",
                  "char","class","const","const_cast",
                  "continue","default","delete","do","double",
                  "dynamic_cast","else","enum","explicit",
                  "export","extern","false","float","for",
                  "friend","goto","if","inline","int","long",
                  "main","mutable","namespace","new",
                  "operator","private","protected","public",
                  "register","reinterpret_cast","return",
                  "short","signed","sizeof","static",
                  "static_cast","struct","switch","template",
                  "this","throw","true","try","typedef",
                  "typeid","typename","union","unsigned",
                  "using","virtual","void","volatile",
                  "wchar_t","while"
                };

    for(int count=0;count<64;count++)
    {
       if(strcmpi(Keywords[count],Token)==0)
      returntrue;
    }

    returnfalse;
 }

 /*************************************************************************//*************************************************************************///-----------------------------  THE END  -------------------------------///*************************************************************************//*************************************************************************/
  
Share: 



Easy Tutor
Easy Tutor author of Program that will read a token from a user and find its type is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!