Sai A Sai A
Updated date Nov 13, 2023
In this blog, we will learn different ways to convert text to uppercase in C. Explore multiple methods to achieve the conversion.

Introduction:

Strings are a fundamental data type in programming, and manipulating them is a common task. In many cases, we might need to convert a string to uppercase, either for consistency or for specific requirements in our code. In this blog, we will explore several methods to convert a string to uppercase in the C programming language.

Method 1: Using a Loop

One of the easiest methods to convert a string to uppercase in C is by using a loop. This method involves iterating through each character of the string and changing lowercase characters to uppercase. Here's a C program demonstrating this approach:

#include <stdio.h>
#include <string.h>

int main() {
    char input[] = "Convert Me To Uppercase";
    int length = strlen(input);

    for (int i = 0; i < length; i++) {
        if (input[i] >= 'a' && input[i] <= 'z') {
            input[i] = input[i] - 32;
        }
    }

    printf("Method 1 Output: %s\n", input);

    return 0;
}
  • We start by including the necessary header files for standard input and output and string manipulation.
  • We define a string called input with the text we want to convert to uppercase.
  • We determine the length of the string using the strlen function.
  • A for loop is used to iterate through each character in the string.
  • Inside the loop, we check if the current character is a lowercase letter (between 'a' and 'z'). If it is, we subtract 32 from its ASCII value to convert it to uppercase.
  • Finally, we print the converted string.

Output:

Method 1 Output: CONVERT ME TO UPPERCASE

Method 2: Using toupper() Function

Another way to convert a string to uppercase in C is by utilizing the toupper() function from the C standard library. This function takes an integer (in this case, a character) and converts it to its uppercase equivalent. Here's a program illustrating this method:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char input[] = "Convert Me To Uppercase";
    int length = strlen(input);

    for (int i = 0; i < length; i++) {
        input[i] = toupper(input[i]);
    }

    printf("Method 2 Output: %s\n", input);

    return 0;
}
  • In this method, we include an additional header file, ctype.h, which contains the toupper() function.
  • We start with the same string input and its length calculation.
  • Inside the loop, we use the toupper() function to convert each character to uppercase directly.
  • The converted string is printed as the output.

Output:

Method 2 Output: CONVERT ME TO UPPERCASE

Method 3: Using a Custom Function

While the previous methods are effective for converting strings to uppercase, sometimes you may need a more customized approach. You might want to create a function that allows you to control the conversion process or apply additional conditions. Here's an example of a custom function to convert a string to uppercase:

#include <stdio.h>
#include <string.h>

void customUpperCase(char* str) {
    int length = strlen(str);
    
    for (int i = 0; i < length; i++) {
        if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = str[i] - 32;
        }
    }
}

int main() {
    char input[] = "Convert Me To Uppercase";
    
    customUpperCase(input);

    printf("Method 3 Output: %s\n", input);

    return 0;
}
  • We define a custom function named customUpperCase that takes a pointer to a character array (string) as its argument.
  • Inside the function, we calculate the length of the string using the strlen function.
  • A loop is used to iterate through each character, and the conversion is done in a manner similar to Method 1.
  • In the main function, we call our custom function with the input string, and the string is converted to uppercase in place.
  • Finally, we print the converted string as the output.

Output:

Method 3 Output: CONVERT ME TO UPPERCASE

Conclusion:

In this blog, we have explored multiple methods for converting strings to uppercase in C. We have started with a basic loop-based approach, demonstrating how to iterate through the string and convert lowercase characters to uppercase. Next, we used the toupper() function to achieve the same result more efficiently. Finally, we introduced the idea of a custom function that allows you to tailor the conversion process to your specific needs.

Comments (0)

There are no comments. Be the first to comment!!!