In: Computer Science
In C++,
The following program reads one character from the keyboard and will display the character in uppercase if it is lowercase and does the opposite when the character is in uppercase. If the character is a digit, it displays a message with the digit.
Modify the program below such that if one of the whitespaces is entered, it displays a message and tells what the character was.
// This program reads one character from the keyboard and
will
// convert it to uppercase. If it is lowercase, convert it to
uppercase.
// If it is a digit display a message with the digit.
#include<iostream>
#include<cctype>
using namespace std;
int main( )
{
char c;
cout << "Enter a character
\n";
cin >> c;
if(isalpha(c))
{ //check to see if it is a letter of
alphabet
if( isupper(c) ) //check
to see if it is uppercase
{
cout << "Your character " << c << " is in
uppercase.";
c = tolower(c);
cout << "Its lowercase case is " << c <<
endl;
}
else
{
cout << "Your character " << c << " is in
lowercase.";
c = toupper(c);
cout << "Its uppercase is " << c << endl;
}
}
else
{
cout << "Your character " << c << " is a
digit.\n";
}
return 0;
}
C++ Program:
// This program reads one character from the keyboard and
will
// convert it to uppercase. If it is lowercase, convert it to
uppercase.
// If it is a digit display a message with the digit.
#include<iostream>
#include<cctype>
using namespace std;
int main( )
{
char c;
cout << "Enter a character \n";
// Reading a character using get function such that a
single character including a single white space can also be
read
cin.get(c);
if(isalpha(c))
{ //check to see if it is a letter of alphabet
if( isupper(c) ) //check to see if it is uppercase
{
cout << "Your character " << c << " is in
uppercase.";
c = tolower(c);
cout << "Its lowercase case is " << c <<
endl;
}
else
{
cout << "Your character " << c << " is in
lowercase.";
c = toupper(c);
cout << "Its uppercase is " << c << endl;
}
}
//Checking for whitespace
else if(c == ' ')
{
//Printing message to user
cout << "\nYour character " << c << " is a
whitespace.\n";
}
else
{
cout << "Your character " << c << " is a
digit.\n";
}
return 0;
}
_____________________________________________________________________________________________
Sample Run: