How to Convert Strings to Uppercase with strupr() in C Programming
The strupr() function changes a string’s case to uppercase. The string that needs to be transformed is the only argument required by the function, which is specified in the <string.h> header file. This article will go into great depth on how to use strupr() to convert strings to uppercase.
The basic syntax of strupr() is:
The string that has to be converted to uppercase is sent as the only input to the strupr() method. The function returns a pointer to the same string in uppercase.
Now, let’s examine how to utilize the strupr() method to change a string to uppercase:
In the above code, we first declare a character array called str with a size of 100. The user’s string is then read using the fgets() method. The strupr() method is then used to transform the string to uppercase. The strupr() method receives the str array as an input. Lastly, we use the printf() function to output the final uppercase text.
Output
Note that the strupr() function modifies the original string. Before calling the strupr() method, you should make a duplicate of the original string if you need to keep it intact.
It is important to note that the strupr() function only works with ASCII characters. It will not work with extended ASCII characters or Unicode characters. If the input string contains extended ASCII or Unicode characters, the output of the strupr() function may be unpredictable.
Create Custom strupr() Function in C Programming
In the following example a custom strupr() function is created which is converting a lowercase string to uppercase:
Output
Conclusion
In C programming, it’s frequently necessary to convert strings to uppercase, and the strupr() function makes it simple to do so. The <string.h> header file must be present in order to use the strupr() method. The function must be called with the string as a parameter, and the original string must be preserved if required. However, when working with specific character types, it is essential to understand its limitations.