C++ Program to Convert String to Uppercase with Explanation
C++
Easy
Strings
44 views
1 min read
185 words
This problem helps you practice core C++ fundamentals in a practical way. It builds intuition around uppercase, lowercase, letter. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Convert all lowercase letters to uppercase.
Real Life: Like CAPS LOCK on keyboard.
Input Format
A string S (may contain lowercase and uppercase letters).
Output Format
String with all lowercase letters converted to uppercase.
Constraints
• String length ≥ 1
• Only lowercase letters are converted
• Other characters remain unchanged
Concept Explanation
Lowercase letters (a–z) are converted to uppercase (A–Z).
This works like pressing CAPS LOCK on a keyboard.
Step-by-Step Logic
1.Take input string S.
2.Traverse the string character by character.
3.For each character:
• If it is between 'a' and 'z':
• Convert it to uppercase by subtracting 32 from its ASCII value
(or use built-in logic).
4.Replace the character with its uppercase form.
5.Continue till the end of the string.
6.Print the updated string.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
void string_q2_uppercase() {
string text = "hello world";
cout << "Original: " << text << endl;
for(int i = 0; text[i] != '�'; i++) {
if(text[i] >= 'a' && text[i] <= 'z') {
text[i] = text[i] - 32; // Convert to uppercase
}
}
cout << "Uppercase: " << text << endl;
}
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Convert all lowercase letters to uppercase.
Real Life: Like CAPS LOCK on keyboard.
Input / Output
Input
A string S (may contain lowercase and uppercase letters).
Output
String with all lowercase letters converted to uppercase.
Constraints
• String length ≥ 1
• Only lowercase letters are converted
• Other characters remain unchanged
Explanation
Concept Explanation
Lowercase letters (a–z) are converted to uppercase (A–Z).
This works like pressing CAPS LOCK on a keyboard.
Step-by-Step Explanation
1.Take input string S.
2.Traverse the string character by character.
3.For each character:
• If it is between 'a' and 'z':
• Convert it to uppercase by subtracting 32 from its ASCII value
(or use built-in logic).
4.Replace the character with its uppercase form.
5.Continue till the end of the string.
6.Print the updated string.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
void string_q2_uppercase() {
string text = "hello world";
cout << "Original: " << text << endl;
for(int i = 0; text[i] != '�'; i++) {
if(text[i] >= 'a' && text[i] <= 'z') {
text[i] = text[i] - 32; // Convert to uppercase
}
}
cout << "Uppercase: " << text << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!