Checking type of Character entered By Using Functions

22:46 0 Comments A+ a-

Problem:
Write a program that contains following functions:
bool isDigit (char); // returns true if the parameter is a digit.
bool isArithmeticOperator(char); // returns true if the parameter is an arithmetic operator.
bool isSmall(char); // returns true if the parameter is a small alphabet.
bool isCapital(char); // returns true if the parameter is a capital alphabet.
Display a menu to the user and act according to the user’s choice.
Sample Run 1:
-----------------------> Menu <---------------------
To Enter a Digit press ‘d’ or ‘D’
To Enter an Arithmetic Operator press ‘a’ or ‘A’
To Enter a Small letter press ‘s’ or ‘S’
To Enter a Capital letter press ‘c’ or ‘C’
To Exit press ‘e’ or ‘E’
------------------------------------------------------
Enter your choice: S
------------------------------------------------------
Enter Small Letter: d

Yes it is a small letter.
Solution Code:
# include <iostream>
using namespace std;
bool isDigit (char c)
{

if(c>=48 && c<=57)// can also use c>=0||c<=9
{
return true;
}
else
{
return false;
}
}
bool isArithmeticOperator(char c)
{

if(c==37 || c==42 || c==43 || c==45 || c==47) //can also use c=='%' and so on
{
return true;
}
else
{
return false;
}
}
bool isSmall(char c)
{

if(c>=97 && c<=122) //can also use c>='a'&&c<='z'
{
return true;
}
else
{
return false;
}
}
bool isCapital(char c)
{

if(c>=65 && c<=90)
{
return true;
}
else
{
return false;
}
}

int main()
{
char choice;
char c;
cout<<"-----------------------> Menu <---------------------"<<endl;
cout<<"To Enter a Digit press 'd' or 'D'"<<endl;
cout<<"To Enter an Arithmetic Operator press 'a' or 'A'"<<endl;
cout<<"To Enter a Small letter press 's' or 'S'"<<endl;
cout<<"To Enter a Capital letter press 'c' or 'C'"<<endl;
cout<<"To Exit press 'e' or 'E'"<<endl;
cout<<"----------------------------------------------------"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
cout<<"----------------------------------------------------"<<endl;
if (choice == 'd' || choice =='D')
{
cout<<"Please Enter a Digit : ";
cin>>c;
if(isDigit(c) ==true)
{
cout<<"Number is a digit"<<endl;
}
else
{
cout<<"Number is not a digit"<<endl;
}
}
else if(choice =='a' ||choice== 'A')
{
cout<<"Please Enter a Arithmetic Operator : ";
cin>>c;
if(isArithmeticOperator(c) ==true)
{
cout<<"Character is a Arithmetic Operator"<<endl;
}
else
{
cout<<"Character is not a Arithmetic Operator"<<endl;
}

}
else if(choice =='s' ||choice== 'S')
{
cout<<"Please Enter a Small letter : ";
cin>>c;
if(isSmall(c) ==true)
{
cout<<"Alphabet is a Small letter"<<endl;
}
else
{
cout<<"Alphabet is not a Small letter"<<endl;
}

}
else if(choice =='c' ||choice== 'C')
{
cout<<"Please Enter a Capital letter : ";
cin>>c;
if(isCapital(c) ==true)
{
cout<<"Alphabet is a Capital letter"<<endl;
}
else
{
cout<<"Alphabet is not a Capital letter"<<endl;
}

}
else if(choice =='e' ||choice== 'E')
{
return 0;
}

else
{
cout<<"The choice entered is Wrong... Try Later"<<endl;
}



system("pause");
}