Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C++ that asks the user for a sentence and then switches all l

ID: 3811137 • Letter: W

Question

Write a program in C++ that asks the user for a sentence and then switches all lowercase letters to uppercase and vice versa. You will accomplish the same task in two different ways. In the first case, use character functions tolower() and toupper() to make the change. In the second case, use character arithmetic to make the change (hint: find an ASCII table and find out how much to add/subtract as needed to change from ‘A’ to ‘a’, ‘B’ to ‘b’, etc). You will need to use casting for this part. Display both results – they should be the same.

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;

int main()
{
string s;
cout<<"Enter the sentence: ";
getline(cin , s);
cout<<"Using character functions: "<<endl;
for(int i=0; i<s.length(); i++){
if(s[i] >='a' && s[i]<='z') {
cout<<(char)toupper(s[i]);
}
else if(s[i] >='A' && s[i]<='Z') {
cout<<(char)toupper(s[i]);
}
else{
cout<<s[i];
}
}
cout<<endl;

cout<<"Using Ascii: "<<endl;
for(int i=0; i<s.length(); i++){
if(s[i] >='a' && s[i]<='z') {
cout<<(char)(s[i]-32);
}
else if(s[i] >='A' && s[i]<='Z') {
cout<<(char)(s[i]+32);
}
else{
cout<<s[i];
}
}
cout<<endl;
return 0;
}

Output:

sh-4.2$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                                   

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter the sentence: aaabbSEEEB DDD aaSE                                                                                                                                                                                                                                

Using character functions:                                                                                                                                                                                                                                             

AAABBSEEEB DDD AASE                                                                                                                                                                                                                                                    

Using Ascii:                                                                                                                                                                                                                                                           

AAABBseeeb ddd AAse

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote