Convert Uppercase Letters to Lowercase in a Character Array

To convert uppercase letters to lowercase in a character array in C, we iterate through each character and use the built-in tolower() function from ctype.h or manually adjust the ASCII values by adding 32. This ensures that all uppercase characters (A-Z) are converted to their lowercase equivalents (a-z) while leaving other characters unchanged.


Examples of Converting Uppercase Letters to Lowercase

1. Using tolower() Function to Convert Uppercase to Lowercase

In this example, we will convert uppercase letters to lowercase using the tolower() function from the ctype.h library.

main.c

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

int main() {
    char str[] = "HELLO WORLD";
    int i = 0;

    // Convert uppercase letters to lowercase
    while (str[i] != '\0') {
        str[i] = tolower(str[i]);
        i++;
    }

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

Explanation:

  1. We declare a character array str[] initialized with “HELLO WORLD”.
  2. We use a while loop to iterate through each character until we reach the null terminator '\0'.
  3. Inside the loop, we use the tolower() function to convert uppercase letters to lowercase.
  4. The modified string is then printed using printf().

Output:

Converted String: hello world

2. Using ASCII Value Adjustment to Convert Uppercase to Lowercase

In this example, we will manually convert uppercase letters to lowercase by adding 32 to their ASCII values.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char str[] = "C PROGRAMMING";
    int i = 0;

    // Convert uppercase letters to lowercase using ASCII values
    while (str[i] != '\0') {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = str[i] + 32;
        }
        i++;
    }

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

Explanation:

  1. We declare a character array str[] initialized with “C PROGRAMMING”.
  2. We iterate through each character using a while loop until we reach '\0'.
  3. We check if the character is uppercase using if (str[i] >= 'A' && str[i] <= 'Z').
  4. If true, we convert it to lowercase by adding 32 to its ASCII value.
  5. The modified string is printed using printf().

Output:

Converted String: c programming

3. Handling Mixed Case Strings while Converting Uppercase to Lowercase

In this example, we handle strings that contain a mix of uppercase, lowercase, and special characters while ensuring that only uppercase letters are converted.

main.c

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

int main() {
    char str[] = "Apple, Banana!";
    int i = 0;

    // Convert uppercase letters to lowercase while preserving other characters
    while (str[i] != '\0') {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = tolower(str[i]);
        }
        i++;
    }

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

Explanation:

  1. We declare a character array str[] initialized with “Hello, C Programming!”.
  2. We use a while loop to iterate through each character.
  3. Uppercase characters are converted using tolower(), while other characters remain unchanged.
  4. The final result is displayed using printf().

Output:

Converted String: apple, banana!

Conclusion

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

  1. Using tolower() function: The simplest and most efficient method.
  2. Using ASCII value adjustment: Manually converting by adding 32 to the ASCII values.
  3. Handling mixed case strings: Ensuring that only uppercase letters are converted while preserving other characters.