C Programming

How to Convert Strings to Uppercase with strupr() in C Programming?

Changing the text case of a string is a common need while writing in the C language. Converting a string to uppercase is one of the most often performed tasks. We have a method in the C language called strupr() that allows us to transform strings to uppercase.

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:

char* strupr(char* str);

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:

#include <stdio.h>

#include <string.h>

int main()

{

  char str[100];

printf("Enter a string: ");

fgets(str, 100, stdin);

strupr(str);

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

  return 0;

}

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

Text Description automatically generated

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:

#include <ctype.h>

#include <string.h>

void main()

{

  char string[] = { "linuxhint" };

  printf("%s\n", string);

  strupr(string);

  printf("%s\n", string);

}

void strupr(char *p)

{

  while (*p)

{

    *p = toupper(*p);

    p++;

}

}

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.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.