Lowercase to Uppercase Converter using C++
Lowercase to Uppercase Converter
Write a program that lets the user enter a string into a character array. The program
should then convert all the lowercase letters to uppercase. (If a character is already
uppercase, or is not a letter, it should be left alone.) Hint: Consult the ASCII chart in
Appendix B. Notice that the lowercase letters are represented by the ASCII codes 97
through 122. If you subtract 32 from any lowercase character’s ASCII code, it will
yield the ASCII code of the uppercase equivalent.
Answer:
// Lowercase to Uppercase Converter
#include <iostream>
using namespace std;
int main()
{
// Constant for the size of a line of input
const int SIZE = 81;
// Constant for the minimum lowercase ASCII code
const int MIN_LOWERCASE = 97;
// Constant for the maximum lowercase ASCII code
const int MAX_LOWERCASE = 122;
// Array to hold a line of input
char line[SIZE];
// Prompt the user to enter a string.
cout << "Enter a string of 80 or fewer characters:\n";
cin.getline(line, 81);
// Convert all lowercase characters to uppercase
for (int count = 0; count < SIZE; count ++) { // If the character is within the range of a // lowercase ASCII code, then subtract 32 from // it to convert it to an uppercase character. if (line [count] >= MIN_LOWERCASE &&
line [count] <= MAX_LOWERCASE)
{
line [count] = line [count] - 32;
}
}
// Display the converted line of input.
cout << line << endl;
return 0;
}
Leave a reply