C program to Convert String to Uppercase

How to write a C Program to Convert String to Uppercase without using strupr function. We can achieve this in multiple ways, but we will discuss four different approaches: using For Loop, While Loop, Functions, and ASCII Values.

C program to Convert String to Uppercase without using strupr()

This program allows the user to enter any character array. Next, it will use For Loop to iterate every character in that string and convert them to Uppercase.

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str1[100];
  	int i;
 
  	printf("\n Please Enter a String that you want to Convert into Uppercase :  ");
  	gets(Str1);
  	
  	for (i = 0; Str1[i]!='\0'; i++)
  	{
  		if(Str1[i] >= 'a' && Str1[i] <= 'z')
  		{
  			Str1[i] = Str1[i] -32;
		}
  	}

  	printf("\n The given String in Upper Case = %s", Str1);
  	
  	return 0;
}
C program to Convert String to Uppercase without using strupr 1

We declared a character array of size 100. The below printf and gets statements to ask the user to enter any word and assign the user entered a char array to the Str variable. Please refer to the strupr function.

In the next line, we have For Loop to iterate and convert the string to uppercase,

for (i = 0; Str1[i]!='\0'; i++)
{
	if(Str1[i] >= 'a' && Str1[i] <= 'z')
	{
		Str1[i] = Str1[i] -32;
	}
}

User Entered = “WorLd”

First Iteration: for (i = 0; Str[0]!=’\0′; 0++)
(Str[0] != ‘\0’) condition is TRUE because Str[0] = W
if(Str1[i] >= ‘a’ && Str1[i] <= ‘z’) – Condition is False

Second Iteration: for (i = 1; Str[1]!=’\0′; 1++)
(Str[1] != ‘\0’) condition is TRUE because Str[1] = o
if(Str1[i] >= ‘a’ && Str1[i] <= ‘z’) – Condition is True
Str1[i] = Str1[i] – 32 => 111 – 32
Str1[i] = 79
79 is the ASCII code for the capital O.

Do the same for the remaining C Programming iterations

C program to Convert String to Uppercase using For Loop

This program to convert string to uppercase is the same as above. Still, this time we are using ASCII values inside the If Statement. I suggest you refer to ASCII Table to understand the codes.

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str1[100];
  	int i;
 
  	printf("\n Please Enter any text :  ");
  	gets(Str1);
  	
  	for (i = 0; Str1[i]!='\0'; i++)
  	{
  		if(Str1[i] >= 97 && Str1[i] <= 122)
  		{
  			Str1[i] = Str1[i] -32;
		}
  	}

  	printf("\n Upper Case = %s", Str1);
  	
  	return 0;
}
 Please Enter any text :  c programs

 Upper Case = C PROGRAMS

C program to Convert String to Uppercase Using While Loop

This example is the same as earlier, but this time we are using a While Loop (Just replacing For Loop with While Loop).

#include <stdio.h> 

int main()
{
	char str[100];
	int i;
 
	printf("\n Please Enter a Lower Sentence :  ");
	gets(str);
 
 	i = 0;
	while (str[i] != '\0') 
	{
    	if (str[i] >= 'a' && str[i] <= 'z') 
		{
        	str[i] = str[i] - 32;
    	}
      	i++;
	}
 
	printf("\n Upper Case = %s", str);
 
	return 0;
}
 Please Enter a Lower Sentence :  tutorial gateway

 Upper Case = TUTORIAL GATEWAY

C program to Convert String to Uppercase Using Functions

This program to convert string to uppercase uses the Functions concept to separate the logic from the main.

#include <stdio.h> 

void String_Upper(char []);
 
int main()
{
	char str[100];
 
	printf("\n Please Enter any lower text : ");
	gets(str);
 
	String_Upper(str);
 
	printf("\n Upper Case = %s", str);
 
	return 0;
}
 
void String_Upper(char str[]) 
{
	int i = 0;
 
	while (str[i] != '\0') 
	{
    	if (str[i] >= 'a' && str[i] <= 'z') 
		{
        	str[i] = str[i] - 32;
    	}
      	i++;
	}
}
 Please Enter any lower text : Learn C Programming

 Upper Case = LEARN C PROGRAMMING

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.