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...
(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.
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 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”....
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt and determines the following: The number of uppercase letters in the file The number of lowercase letters in the file The number of digits in the file Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output, preceded by the following...
Write a program that reads three values from the keyboard representing, respectively, an investors name, an...
Write a program that reads three values from the keyboard representing, respectively, an investors name, an investment amount, and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate) . Your program should calculate and output (in currency notation) the future value of the investment in 10, 2 20 an 30 years using the following formula:   Future value =investment(1+interest rate)year Example of a test run: Enter...
Write a program that reads an integer, a list of words, and a character.
13.14 LAB: Contains the characterWrite a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character.Ex: If the input is:4 hello zoo sleep drizzle zthen the output is:zoo drizzleIn c++ 
Write a program that reads three double numbers from the keyboard representing, respectively, the three coefficients...
Write a program that reads three double numbers from the keyboard representing, respectively, the three coefficients a, b, and c of a quadratic equation. Solve the equation using the following formulas: x1 = ( - b + square root (b2 – 4ac)) / (2a), x2 = ( - b + square root (b2 – 4ac)) / (2a) Run your program on the following sample values: a=1.0, b=3.0,c=2.0 a=0.5, b=0.5,c=0.125 a=1.0, b=3.0,c=10.0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT