Convert Lowercase Letters to Uppercase in a Character Array

To convert lowercase letters to uppercase in a character array in C, we iterate through the array and modify each character using the toupper() function from the ctype.h library or by subtracting 32 from ASCII values of lowercase letters. This ensures all lowercase letters are converted while non-alphabetic characters remain unchanged.


Examples of Converting Lowercase to Uppercase

1. Convert Lowercase to Uppercase Using toupper() Function

In this example, we will iterate through a character array (string) and convert each lowercase letter to uppercase using the toupper() function from ctype.h.

main.c

</>
Copy
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[] = "hello world!";
    int i = 0;

    // Iterate through the character array
    while (str[i] != '\0') {
        str[i] = toupper(str[i]); // Convert lowercase to uppercase
        i++;
    }

    printf("Uppercase String: %s\n", str);
    return 0;
}

Explanation:

  1. We declare a character array str[] initialized with the string "hello world!".
  2. We use an integer variable i to traverse the array.
  3. Inside a while loop, we check if the current character is not the null terminator '\0'.
  4. We apply the toupper() function, which converts lowercase letters to uppercase.
  5. The loop continues until we reach the end of the string.
  6. The modified string is printed using printf().

Output:

Uppercase String: HELLO WORLD!

2. Convert Lowercase to Uppercase Without toupper()

In this example, we convert lowercase letters to uppercase manually by subtracting 32 from the ASCII value of each lowercase character.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char str[] = "c programming";
    int i = 0;

    // Iterate through the character array
    while (str[i] != '\0') {
        if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = str[i] - 32; // Convert lowercase to uppercase
        }
        i++;
    }

    printf("Uppercase String: %s\n", str);
    return 0;
}

Explanation:

  1. We declare a character array str[] initialized with "c programming".
  2. We use an integer i to traverse the array.
  3. Inside a while loop, we check if the character is lowercase ('a' to 'z').
  4. If it is lowercase, we subtract 32 from its ASCII value to convert it to uppercase.
  5. The loop continues until we reach the null terminator '\0'.
  6. The modified string is printed using printf().

Output:

Uppercase String: C PROGRAMMING

3. Convert Lowercase to Uppercase Using for Loop

In this example, we will use a for loop instead of a while loop to traverse the character array and convert each lowercase letter to uppercase.

main.c

</>
Copy
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[] = "Apple Banana";
    int length = sizeof(str) / sizeof(str[0]);

    // Convert lowercase letters to uppercase using for loop
    for (int i = 0; i < length; i++) {
        str[i] = toupper(str[i]);
    }

    printf("Uppercase String: %s\n", str);
    return 0;
}

Explanation:

  1. We declare a character array str[] with the string "example text".
  2. We determine the string length using sizeof(str) / sizeof(str[0]).
  3. We use a for loop to iterate over each character.
  4. Inside the loop, we apply toupper() to convert lowercase letters.
  5. The final uppercase string is printed using printf().

Output:

Uppercase String: APPLE BANANA

Conclusion

We have explored different ways to convert lowercase letters to uppercase in a character array:

  1. Using toupper() from ctype.h.
  2. Manually converting using ASCII subtraction.
  3. Using a for loop instead of while.