C String Uppercase: strupr()

In C programming, the strupr() method is used to make all the characters in a string uppercase. The <string.h> header file contains the strupr() function, and it is a component of the C Standard Library. A pointer to the null-terminated string (an array of characters) is the only argument required by the function. When the strupr() function is used, all alphabetic characters in the string are transformed to uppercase, while non-alphabetic characters are left alone.

The strupr(string) function returns string characters in uppercase. Let's see a simple example of strupr() function.

Output

Enter string: javatpoint
String is: javatpoint
Upper String is: JAVATPOINT

Explanation:

The program begins by incorporating the required header files: string.h for functions related to strings and stdio.h for input/output operations. It defines a 20-character str array as its size. The user-provided input string will be kept in this array.

A prompt message asking the user to enter a string is shown using the printf() function.

The user-input string is received from the console and stored in the str array using the gets() method. It is crucial to remember that using gets() function might result in buffer overflow problems, making it dangerous. It is preferable to utilize fgets() to reliably read input in real-world applications.

After that, the original input string is shown by the program using printf() function. The intriguing portion is about to start. The str array is sent as an argument when the strupr() method is used. All alphabetic characters in the string will be converted to uppercase using this function, which makes the changes in-place.

After that, the updated (uppercase) string is printed by the program using the printf() function.

Benefits of strupr() function:

  1. Simple Uppercase Conversion: Using the strupr() method, every character in a string may be easily converted to uppercase without the need for any additional character manipulation.
  2. By modifying the original string while it is still in existence, the function avoids the need to create a new string specifically to hold the transformed result.
  3. Convenience in Text Processing: It is very helpful when case-insensitive text comparisons or operations are needed.

Cautions:

  1. As previously explained, the gets () method is unsafe to employ since it doesn't verify the size of the buffer and can result in buffer overflows. Use fgets() to safely read input instead.
  2. Locale Consideration: Since different locales may have rules for case conversions, the behavior of strupr() may vary depending on the current locale settings. It's crucial to confirm that your locale settings are acceptable for your use case if you see unexpected behavior.
  3. It's important to note that the strupr() function is not a part of the C standard library. It may not be present in all C contexts, despite being often accessible on many C compilers (such as Borland C, Turbo C, etc.).

Conclusion:

In conclusion, the C strupr() function provides a practical and simple method for changing all the characters in each string to uppercase. Although it is not a part of the standard C library, it is frequently present in many C settings and can be handy for case-insensitive string manipulation. Nevertheless, the feature must be used with caution.

The unsafe gets() function, which can result in buffer overflows, is one of the main issues with the presented code example. It is advised to utilize methods like fgets() to read input with a designated buffer size to ensure secure input handling.

Additionally, strupr()'s behavior could be impacted by the current locale settings, which may have an impact on the conversion process based on certain language regulations. For better control over case conversion, it's recommended to select the proper locale or make use of common character manipulation methods like toupper() function.


Next TopicC strstr()


Latest Courses