MeetCode - Programming Platform | MeetCode - Programming Solutions Platform

C++ Program to Convert String to Uppercase with Explanation

C++ Easy Strings 44 views
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.
Back to Questions

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; }

Output Example

Input:
Hello World
Output:
HELLO WORLD

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).

Notes & Extra Practice

Solutions (0)

No solutions submitted yet. Be the first!

Prev Next