Example
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nUpper String is: %s",strupr(str));
return 0;
}
Output
Enter string: logic practice
String is: logic practice
Upper String is: LOGIC PRACTICETECH
Explanation:
The process starts by including the necessary header files: string.h for string-related functions and stdio.h for input/output tasks. A str array with a capacity of 20 characters is declared to store the user's input string.
A prompt is displayed to the user requesting input of a string through the printf function.
The string entered by the user via the console is captured and saved in the str array using the gets function. It's important to note that employing gets can lead to buffer overflow issues, posing a security risk. In practical scenarios, it is recommended to opt for fgets for secure input reading.
Following this, the program displays the initial input string using the printf function. The upcoming section is where things get interesting. The str array is passed as a parameter for the strupr function. Utilizing this method will transform all alphabetical characters within the string to uppercase, effectively modifying the string in situ.
Following this, the modified (uppercase) string is displayed by the software through the printf method.
Benefits of strupr function:
- 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.
- 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.
- Convenience in Text Processing: It is very helpful when case-insensitive text comparisons or operations are needed.
- 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.
- 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.
- 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.).
Cautions:
Conclusion:
In summary, the C strupr function offers a convenient and straightforward approach to converting all characters within a string to uppercase. While not a standard C library function, it is commonly found in various C environments and can be useful for tasks involving case-insensitive string operations. However, it is important to exercise caution when utilizing this functionality.
The insecure gets function, known for its vulnerability to buffer overflows, poses a significant risk in the provided code snippet. It is recommended to employ alternatives such as fgets for input reading, specifying a buffer size to enhance input security.
Furthermore, the functionality of strupr could be influenced by the existing locale configurations, potentially affecting the conversion procedure according to specific linguistic guidelines. To enhance precision in case modification, it is advisable to specify the appropriate locale or utilize standard character manipulation techniques such as the toupper function.