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 string or character array. Next, it will use For Loop to iterate every character in that string, and convert them to Uppercase.

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

#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. Below statements asks the user to enter any word, and assign the user entered a string to Str variable. Please refer to strupr function.

printf("\n Please Enter any String :  ");
gets(Str);

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

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

User Entered the String “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 capital O

Do the same for 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 to refer ASCII Table to understand the codes

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

#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] >= 97 && Str1[i] <= 122)
  		{
  			Str1[i] = Str1[i] -32;
		}
  	}

  	printf("\n The given String in Upper Case = %s", Str1);
  	
  	return 0;
}

C string to uppercase output

 Please Enter a String that you want to Convert into Uppercase :  c programs

 The given String in Upper Case = C PROGRAMS

C program to Convert String to Uppercase Using While Loop

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

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

#include <stdio.h> 

int main()
{
	char str[100];
	int i;
 
	printf("\n Please Enter a String to convert it into Uppercase :  ");
	gets(str);
 
 	i = 0;
	while (str[i] != '\0') 
	{
    	if (str[i] >= 'a' && str[i] <= 'z') 
		{
        	str[i] = str[i] - 32;
    	}
      	i++;
	}
 
	printf("\n The given String in Upper Case = %s", str);
 
	return 0;
}

C string uppercase output

 Please Enter a String to convert it into Uppercase :  tutorial gateway

 The given String in Upper Case = TUTORIAL GATEWAY

Program to Convert String to Uppercase Using Functions

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

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

#include <stdio.h> 

void String_Upper(char []);
 
int main()
{
	char str[100];
 
	printf("\n Please Enter a string to convert it into Uppercase : ");
	gets(str);
 
	String_Upper(str);
 
	printf("\n The given String in Upper Case = %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++;
	}
}
 Please Enter a string to convert it into Uppercase : Learn C Programming

 The given String in Upper Case = LEARN C PROGRAMMING