Question

In: Computer Science

In C++, The following program reads one character from the keyboard and will display the character...

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;
}

Solutions

Expert Solution

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:


Related Solutions

Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
ite a C program that prompts for and reads in a non-negative integer from the keyboard....
ite a C program that prompts for and reads in a non-negative integer from the keyboard. Read it into an int variable x. Then display the 32 bits in x from the lsb to the msb (so the display will show the value in x in binary with the bits in reverse order). For example, if you input 6, then your program displays 00000000000000000000000000000110 Use the algorithm given in class (repeatedly divide by 2). Use the / and % operators....
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
Question 1: Write a C program that reads a date from the keyboard and tests whether...
Question 1: Write a C program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid month...
Write an LC-3 program that will repeatedly read a character from the keyboard. For each character...
Write an LC-3 program that will repeatedly read a character from the keyboard. For each character read in, your program will print a neat message that echoes the input value, and the ASCII character code of the input character in hexadecimal. It will run forever: no HALT or End of processing is required. For example: Please press a key: You pressed 'z' which is x7A Please press a key: You pressed '@' which is x40 Please press a key: You...
Write a program in python that reads the elements of a set from the keyboard, stores...
Write a program in python that reads the elements of a set from the keyboard, stores them in a set, and then determines its powerset. Specifically, the program should repeatedly ask the user: Enter one more element ? [Y/N] If the user answers Y then an new element is read from the keyboard: Enter the new element in the set: This cycle continues until the user answers N to the first question. At that point the program shall compute the...
Write a C++ function called parse that reads one line of user input from the keyboard...
Write a C++ function called parse that reads one line of user input from the keyboard and creates an array of the strings found in the input.  Your function should be passed the array and a reference variable that is to be assigned the length of the array.  Prompt the user and read the input from within the function. For example:  If the user inputs copy this that, the resulting array would have length 3 and contain the strings “copy”, “this”, and “that”....
Use C language Write a program that reads in a series of lines of input character...
Use C language Write a program that reads in a series of lines of input character by character (using getchar()). The first line of the input contains an integer which specifies the number of remaining lines of input, each of which contains a floating point number. The integer value on the first line can be read with scanf(), but all of the following lines can only be read with getchar(). Each line after the first contains a single floating point...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT