Code:
#include <iostream.h>
#include <conio.h>
enum MessageIds {UPR_CHAR, LWR_CHAR, NUM, VOWEL_LWR, VOWEL_UPR, PUNCT, ARITH};
void Messages(MessageIds, int, char);
bool StringSearch(const char String[], char Temp, int &Pos);
int main(void)
{
const int START_ALPHA_UPR = 65, END_ALPHA_UPR = 90, START_ALPHA_LWR = 97,
END_ALPHA_LWR = 122, NUM_LWR = 48, NUM_UPR = 57,
VOWEL_LWR_POS = 4, ESC = 27;
const char VOWELS[] = "aeiouAEIOU\0", PUNCTUATION[] = ".,?';:!\0",
ARITHMETIC[] = "-+*/\0";
MessageIds MsgIds;
char Character;
int AsciiVal, Index;
do
{
clrscr();
cout << "Please enter a character (Esc to quit): ";
cin >> Character;
AsciiVal = Character;
if(StringSearch(VOWELS, AsciiVal, Index) && Index <= VOWEL_LWR_POS)
Messages(MsgIds, VOWEL_LWR, AsciiVal); //needs to be first case
else
{
if(Index != 10)
Messages(MsgIds, VOWEL_UPR, AsciiVal);
else
{
if(AsciiVal >= START_ALPHA_UPR && AsciiVal <= END_ALPHA_UPR)
Messages(MsgIds, UPR_CHAR, AsciiVal);
else
{
if(AsciiVal >= START_ALPHA_LWR && AsciiVal <= END_ALPHA_LWR)
Messages(MsgIds, LWR_CHAR, AsciiVal);
else
{
if(AsciiVal >= NUM_LWR && AsciiVal <= NUM_UPR)
Messages(MsgIds, NUM, AsciiVal);
else
{
if(StringSearch(PUNCTUATION, AsciiVal, Index))
Messages(MsgIds, PUNCT, AsciiVal);
else
{
if(StringSearch(ARITHMETIC, AsciiVal, Index))
Messages(MsgIds, ARITH, AsciiVal);
}
}
}
}
}
}
}while(AsciiVal != ESC);
cout << "\nTada........";
getchar();
return 0;
}
void Messages(MessageIds MsgIds, int MsgCode, char Character)
{
switch(MsgCode)
{
case UPR_CHAR:
cout << "\n" << Character << " is a upper case character.";
getchar();
break;
case LWR_CHAR:
cout << "\n" << Character << " is a lower case character.";
getchar();
break;
case NUM:
cout << "\n" << Character << " is a number.";
getchar();
break;
case VOWEL_LWR:
cout << "\n" << Character << " is a lower case letter and a vowel.";
getchar();
break;
case VOWEL_UPR:
cout << "\n" << Character << " is a upper case letter and a vowel.";
getchar();
break;
case PUNCT:
cout << "\n" << Character << " is a punctuation character.";
getchar();
break;
case ARITH:
cout << "\n" << Character << " is a arithmetic operator.";
getchar();
break;
}
}
bool StringSearch(const char String[], char Temp, int &Pos)
{
bool MatchFound = false;
int i = 0;
while(String[i] != '\0' && !MatchFound)
{
if(String[i] == Temp)
{
MatchFound = true;
Pos = i;
}
i++;
}
Pos = i;
return MatchFound;
}