Strupr in C Programming

The C Strupr function is a C String Function, used to convert the given characters or string into Uppercase letters. The syntax of the Strupr function in C language is

strupr(chars);

The above function will accept the characters as the parameter and convert all the characters in a string to uppercase using the built-in String function Strupr in C Programming. Remember, you have to include the #include<string.h> header before using any string function.

Strupr in C Programming Example

The strupr function converts all the characters in a given string to uppercase. This program will help you to understand the same.

#include <stdio.h> 
#include<string.h>
int main()
{
	char str[] = "C ProgramminG Tutorial at Tutorial GateWay";
		
	char str1[] = "c laGUagE";
	char str2[] = "Java Programming Language";
	char str3[] = "c PRogramming WOrld";
	char str4[] = "TrY tO ReAd ThIs SenTEnCe";
			
 	printf("\n Upper Case String is = %s", strupr(str));
 	printf("\n Upper Case String is = %s", strupr(str1));
 	printf("\n Upper Case String is = %s", strupr(str2));
 	printf("\n Upper Case String is = %s", strupr(str3)); 	
  	printf("\n Upper Case String is = %s", strupr(str3)); 		
}
Strupr in C Programming 1

In this strupr in c program, first, we declared five-character arrays str, str1, str2, str3, str5. By using the following statement, we assigned a group of characters to each character array

char str[] = "C ProgramminG Tutorial at Tutorial GateWay";
char str1[] = "c laGUagE";
char str2[] = "Java Programming Language";
char str3[] = "c PRogramming WOrld";
char str4[] = "TrY tO ReAd ThIs SenTEnCe";

Next, we used the C strupr method directly inside the printf statements to print the output. The following C Programming statements will convert the previously declared String into uppercase

printf("\n Upper Case String is = %s", strupr(str));
printf("\n Upper Case String is = %s", strupr(str1));
printf("\n Upper Case String is = %s", strupr(str2));
printf("\n Upper Case String is = %s", strupr(str3)); 	
printf("\n Upper Case String is = %s", strupr(str3));

C Program to convert string to Uppercase without Strupr

This strupr program will help you to understand how to write a C program to convert the given string into the Upper case without using the built-in String Function strupr.

#include <stdio.h> 
void String_Upper(char []);
 
int main()
{
	char str[100];
 
	printf("\n Please Enter a string to convert it into Uppercase\n");
	gets(str);
 
	String_Upper(str);
 
	printf("\n Upper Case String is = %s", str);
 
	return 0;
}
 
void String_Upper(char string[]) 
{
	int i = 0;
 
	while (string[i] != '\0') 
	{
    	if (string[i] >= 'a' && string[i] <= 'z') {
        	string[i] = string[i] - 32;
    	}
      	i++;
	}
}

C uppercase output

 Please Enter a string to convert it into Uppercase
hello C Programming Language

 Upper Case String is = HELLO C PROGRAMMING LANGUAGE

In this C strupr example, the first printf statement will ask the user to enter any name or string. Next, the user-specified string will assign to character array str[100].

printf("\n Please Enter a string to convert it into Uppercase\n");
gets(str);

The following statement will call the function void String_Upper(char string[])

String_Upper(str);

For every string, the special character \0 is added at the end to mark where it ends. So, in the while loop, we check for the same:

while (string[i] != '\0')
  • While loop will continue iterating until it reaches the special character \0, once it reached, the loop will terminate.
  • In the next line, we used the If Statement to check whether the character is between ‘a’ and ‘z’. If the condition is True, we are subtracting 32 from the ASCII value. It is because the ASCII value of a is 97, and A is 65. So, to convert a to A, we are subtracting 32 from it. Please refer ASCII Table article to understand the list of ASCII characters and their decimal, Hexadecimal, and Octal numbers
  • Next line, we increment the value of i.