C++ Primer Plus Chapter 6 Exercise 1

c plus plusChapter 6 brings us to branching statements, also know as if and else statements.  We are also introduced to the cctype library which makes light work of some basic character operations. Exercise 1 ask us to accept keyboard input to @, not to display numbers, convert uppercase to lowercase and vice versa. One other thing you will notice this time is I have not pulled in the entire standard library, I except use it in a case by case basis. This can help solve non-obvious ambiguity errors in our later problems. See my source below for a simple solution:

1. Write a program that reads keyboard input to the @ symbol and that echoes the input
except for digits, converting each uppercase character to lowercase, and vice versa.
(Don’t forget the cctype family.)

#include <iostream>
#include <cctype>

int main()
{
char ch;

std::cout << "Enter your characters: ";

while(std::cin.get(ch) && ch != '@') // While inputting and not an '@'
{
if(isdigit(ch))
continue; // Ignore digits
if(isalpha(ch)) // Is it in the alphabet?
if(islower(ch))
std::cout << char(toupper(ch));
else
std::cout << char(tolower(ch));
else
std::cout << ch;
}
return 0;
}
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s