C strupr() Function
The strupr()
function in C is used to convert all lowercase characters in a string to uppercase. Like the strlwr()
function, strupr()
is not part of the standard C library, but it is commonly available in some implementations, such as Microsoft's C Run-Time Library (CRT).
Syntax of strupr()
The syntax of the strupr()
function is as follows:
Syntax:
char *strupr(char *str);
The function takes one argument:
- str: A pointer to the string whose characters will be converted to uppercase. The string is modified in place.
The function returns a pointer to the modified string with all uppercase characters.
Example of strupr() Function
Here is an example demonstrating how the strupr()
function works. Since it is not part of the standard C library, you may need to implement it or use a compatible library.
Example:
#include <stdio.h>
#include <ctype.h>
// For the standard library functions
// Function to convert string to uppercase (implementation of strupr)
char* strupr(char* str) {
for (int i = 0; str[i]; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - ('a' - 'A'); // Convert to uppercase
}
}
return str;
}
int main()
{
char str[] = "hello world!";
printf("Original String: %s\n", str);
// Convert the string to uppercase
strupr(str);
printf("Uppercase String: %s\n", str);
return 0;
}
Output:
Original String: hello world!
Uppercase String: HELLO WORLD!
Uppercase String: HELLO WORLD!
Explanation of the Example
In this example:
- The function
strupr()
is manually implemented to convert each lowercase character in the string to uppercase. - A loop iterates through each character in the string, and if a character is lowercase (between 'a' and 'z'), it is converted to uppercase by subtracting the difference between 'a' and 'A'.
- The modified string is printed after conversion to uppercase.
Important Notes
- The
strupr()
function is not part of the standard C library, so it may not be available in all C environments. - The function modifies the original string in place, so the original string is changed.
- Ensure that the string is null-terminated to avoid undefined behavior.
- Some C compilers or platforms may provide this function as part of their non-standard libraries (e.g., MSVC). If it's not available, you can implement it as shown in the example.