In this C++ program, we will convert a Uppercase character to lowercase character. We will ask user to enter an uppercase character and then convert it to lowercase character. To convert an uppercase character to lowercase, we will add 32 to the ASCII value of uppercase to get corresponding lowercase character.
-
The difference between ASCII value of Lowercase alphabet and it Uppercase equivalent alphabet is 32.
ASCII value of 'B' is 66
ASCII value of 'b' is 98
'b' - 'B' = 98 - 66 = 32
C++ Program to Convert Uppercase alphabet to Lowercase
#include <iostream> using namespace std; int main() { char c; cout << "Enter an uppercase alphabet\n"; cin >> c; if(c >= 'A' && c <= 'Z'){ // Add 32 to uppercase character to // convert it to lowercase c += 32; cout << "Lowercase Alphabet : " << c; } else { cout << "Not an uppercase Alphabet"; } return 0; }Output
Enter an uppercase alphabet F Lowercase Alphabet : f
Enter an uppercase alphabet g Not an uppercase Alphabet
In above program, we first take a character as input from user using cin and store it in variable c. Then using a if-else statement, we check whether c is uppercase character or not. If c is uppercase alphabet then we add 32 from c to get it's lowercase equivalent character.
C++ Program to Convert Uppercase String to Lowercase String
#include <iostream> #include <cstring> using namespace std; int main(){ char input[100]; int i, j; cout << "Enter a string \n"; cin.getline(input, 500); for(i = 0; input[i] != '\0'; i++){ if(input[i] >= 'a' && input[i] <= 'z'){ // If current character is a lowercase alphabet, // then subtract 32 to convert it to lowercase input[i]-= 32; } } cout << "String without lower alphabets\n" << input; return 0; }Output
Enter a string TecHcRAshCOurSE String without lower alphabets TECHCRASHCOURSE
In above program, we first take a string input from user using cin and store it in a character array "input". Using a for loop, we traverse input string from first character to last character and check whether current character is uppercase or not. If current character is uppercase alphabet then we add 32 to get it's lowercase equivalent character. Finally, we print modified string in screen.
Recommended Posts