Convert a String to Uppercase: flowchart, algorithm and program
This program reads a word into str and walks through it one character at a time, turning each lowercase letter into its uppercase form. It works because in ASCII every lowercase letter has a code exactly 32 higher than its uppercase partner, so subtracting 32 does the conversion.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Print "Enter a word: ".
- Step 3: Read str.
- Step 4: Set i = 0.
- Step 5: If str[i] != '\0', go to step 6; otherwise go to step 9.
- Step 6: If str[i] between 'a' and 'z', go to step 7; otherwise go to step 8.
- Step 7: Set str[i] = str[i] - 32.
- Step 8: Increase i by 1. Go to step 5.
- Step 9: Print str as Uppercase.
- Step 10: Stop.
Pseudocode
BEGIN
PRINT "Enter a word: "
READ str
i ← 0
WHILE str[i] != '\0' DO
IF str[i] between 'a' and 'z' THEN
str[i] ← str[i] - 32
END IF
i++
END WHILE
PRINT str as Uppercase
END
Program in C, Python and Java
Convert a String to Uppercase program in C
#include <stdio.h>
int main() {
char str[100];
int i;
printf("Enter a word: ");
scanf("%s", str);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
}
printf("Uppercase: %s\n", str);
return 0;
}Convert a String to Uppercase program in Python
str1 = input("Enter a word: ")
result = ""
for ch in str1:
code = ord(ch)
if code >= 97 and code <= 122:
code = code - 32
result = result + chr(code)
print("Uppercase:", result)Convert a String to Uppercase program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
String str = sc.next();
String result = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'a' && ch <= 'z') {
ch = (char) (ch - 32);
}
result = result + ch;
}
System.out.println("Uppercase: " + result);
}
}
Sample input: word = Hello
Output: Uppercase: HELLO
How it works
- scanf("%s", str) reads one word (no spaces) into the char array str, and a '\0' is added at the end to mark where the string stops.
- The for loop starts at i = 0 and keeps going while str[i] != '\0', so it visits every character exactly once and stops at the end of the word.
- The condition str[i] >= 'a' && str[i] <= 'z' checks that the character is a lowercase letter. Capital letters, digits and symbols fail this test and are left alone.
- For a lowercase letter, str[i] = str[i] - 32 changes its code to the uppercase one. For example 'e' is 101 and 101 - 32 = 69, which is 'E'.
- After the loop ends, printf prints the modified str, so 'Hello' becomes 'HELLO' (the 'H' was already uppercase and is skipped).
Dry run (trace table)
| Step | Line | Box | str | i | Output |
|---|---|---|---|---|---|
| 1 | 3 | Start | |||
| 2 | 6 | Print "Enter a word: " | Enter a word: | ||
| 3 | 7 | Read str | Hello | ||
| 4 | 8 | i = 0 | Hello | 0 | |
| 5 | 8 | str[i] != '\0'? | Hello | 0 | |
| 6 | 9 | str[i] between 'a' and 'z'? | Hello | 0 | |
| 7 | 8 | i++ | Hello | 1 | |
| 8 | 8 | str[i] != '\0'? | Hello | 1 | |
| 9 | 9 | str[i] between 'a' and 'z'? | Hello | 1 | |
| 10 | 10 | str[i] = str[i] - 32 | HEllo | 1 | |
| 11 | 8 | i++ | HEllo | 2 | |
| 12 | 8 | str[i] != '\0'? | HEllo | 2 | |
| 13 | 9 | str[i] between 'a' and 'z'? | HEllo | 2 | |
| 14 | 10 | str[i] = str[i] - 32 | HELlo | 2 | |
| 15 | 8 | i++ | HELlo | 3 | |
| 16 | 8 | str[i] != '\0'? | HELlo | 3 | |
| 17 | 9 | str[i] between 'a' and 'z'? | HELlo | 3 | |
| 18 | 10 | str[i] = str[i] - 32 | HELLo | 3 | |
| 19 | 8 | i++ | HELLo | 4 | |
| 20 | 8 | str[i] != '\0'? | HELLo | 4 |
Time and space complexity
Time: O(n) · Space: O(1). The loop visits each of the n characters once, doing constant work each time, and converts in place.
Common mistakes
- Subtracting 32 from every character without the 'a' to 'z' check. This breaks capitals, digits and symbols (for example 'H' - 32 becomes a different, wrong character).
- Using i < 100 or a fixed length as the loop condition instead of str[i] != '\0', which processes garbage characters after the word.
- Typing a sentence with spaces and expecting it all to be read. scanf with %s stops at the first space, so only the first word is converted.
Viva questions
Q1. Why do we subtract 32 from a lowercase letter to get the uppercase letter?
In ASCII, 'a' is 97 and 'A' is 65, so every lowercase letter is exactly 32 more than its uppercase letter. Subtracting 32 moves it to the matching capital.
Q2. What does the condition str[i] >= 'a' && str[i] <= 'z' check, and why is it needed?
It checks that the character is a lowercase letter. Without it, digits, capitals or symbols would also lose 32 and turn into wrong characters.
Q3. Why does the loop stop at str[i] != '\0'?
A C string ends with the null character '\0', which scanf adds automatically. The loop runs until it reaches it, so we don't need to know the length in advance.
Q4. What happens if the user enters a word that is already in uppercase, like HELLO, or contains digits like abc123?
Uppercase letters and digits fail the condition and stay unchanged. HELLO prints as HELLO, and abc123 prints as ABC123.
Q5. What is the problem with scanf("%s", str) in this program?
It stops reading at the first space, so only one word is read; 'hello world' gives just 'hello'. It also does not check the size, so more than 99 characters overflows the 100-char array.
Q6. What happens if the user just presses Enter without typing anything, or types an empty input?
scanf finds no word, so str is not set and holds garbage, which makes the output undefined. If it were an empty string, the loop would not run at all, because str[0] would already be '\0'.
Q7. What is the time complexity of this program, and why?
O(n), where n is the length of the string. The loop visits each character once and does constant work per character.
Q8. What would you change to use a library function instead of subtracting 32, or to read a full sentence with spaces?
Replace the subtraction with toupper(str[i]) from <ctype.h>, which also makes the if unnecessary. To read spaces, use fgets(str, sizeof(str), stdin) instead of scanf, which is also safe from overflow.
Frequently asked questions
Why do we subtract 32 to convert lowercase to uppercase?
In ASCII, 'a' is 97 and 'A' is 65, so each lowercase letter is exactly 32 more than its uppercase letter. Subtracting 32 moves it to the matching capital.
Why check str[i] >= 'a' && str[i] <= 'z' first?
The check makes sure only lowercase letters are changed. Without it, characters that are already uppercase, or digits and symbols, would get wrong codes after subtracting 32.
Can I use this program for a sentence with spaces?
Not as written, because scanf("%s") stops reading at the first space. Use fgets to read a whole line; the conversion loop stays the same.