Introduction

In the C programming language, there is no built-in function to convert a string to uppercase directly. However, we can easily achieve this by using the ASCII values of the characters and some basic string manipulation techniques. In this tutorial, we will go through the process of converting a string to uppercase in C.

Step 1: Include the necessary header files

To work with strings in C, we need to include the <stdio.h> and <string.h> header files. The <stdio.h> header file provides input/output functions, while <string.h> provides string manipulation functions.

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

Step 2: Declare a function to convert the string to uppercase

Next, we need to declare a function that takes a string as input and converts it to uppercase. Let’s call this function convertToUpper.

void convertToUpper(char *str);

Step 3: Implement the function to convert the string to uppercase

Inside the convertToUpper function, we will iterate through each character of the string and convert its lowercase characters to uppercase using ASCII values.

void convertToUpper(char *str) {
    int i;

    for(i = 0; str[i]!='\0'; i++) {
        if(str[i]>='a' && str[i]<='z') {
            str[i] = str[i] - 32;
        }
    }
}

Step 4: Test the function

Now let’s test our convertToUpper function by calling it with a sample string. We will also print the converted uppercase string using the printf function.

int main() {
    char str[100];

    printf(""Enter a string: "");
    fgets(str, sizeof(str), stdin);

    // Remove the newline character from the input string
    if (str[strlen(str) - 1] == '\n') {
        str[strlen(str) - 1] = '\0';
    }

    convertToUpper(str);

    printf(""Uppercase string: %s\n"", str);

    return 0;
}

Step 5: Compile and run the program

Save the code to a C file, such as convertToUpper.c, and compile it using a C compiler. For example, if you are using the GNU GCC compiler, you can run the following command in the terminal:

gcc -o convertToUpper convertToUpper.c

After successfully compiling the program, run it by executing the generated executable:

./convertToUpper

You will be prompted to enter a string. After entering the string, the program will convert it to uppercase and display the result.

I hope you found this tutorial helpful! By following the steps outlined above, you can easily convert a string to uppercase in the C programming language. Feel free to experiment with different input strings and explore further string manipulation techniques in C.