Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Convert Uppercase to Lowercase in C++ Question

$
0
0
#include<iostream>
using namespace std;

int main()
{

	char uppercase;
	int offset = 'a' - 'A';
	char lowercase;

	cout<<"Enter an uppercase letter:  ";
	cin>>uppercase;

	if(uppercase>='A' && uppercase<='Z')
	lowercase = (char)(uppercase + offset);

	else
	lowercase= uppercase;

	cout<<"The lowercase letter is:  "<<lowercase << endl;

	return 0;
}




Is there any way to write this program without the 'if' and the 'else'? Basically like these descriptions:

STEP 1: Prompt the user to enter an uppercase letter

This means we have to output a prompt to the console using the cout command, which we already know how to use. In STEP 2, we need to use the value that the user enters.

Where do we store the user’s entry? How do we store it? To store it, we need a variable, and the programming assignment already calls it uppercase. Before we can use it, we need to declare it though. I personally like to declare my variables at the beginning of my programs. I would declare uppercase as a char even before STEP 1. Once we have the variable declared, we would need to store the user’s entry into that variable. How? We would use the cin command, which we already know how to use.

STEP 2: Convert it to a lowercase letter

Here, we simply use the code provided with the programming assignment.

STEP 3: Display the result to the console

Here, we simply use the cout command to display our result. To see how this output should look, just look at the sample run included with the exercise in the book.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>