C program to Toggle Case of all Characters in a String

In this tutorial, i am going to show you how to toggle case of all or each characters in a string with the help of for loop, while loop, ascii value, and function in c programs.

All C Programs to Toggle Case of all Characters in a String

  • C program to Toggle Case of all Characters in a String using For Loop
  • C program to Toggle Case of all Characters in a String using While Loop
  • C program to Toggle Case of all Characters in a String using Ascii value
  • C program to Toggle Case of all Characters in a String using Function

C program to Toggle Case of all Characters in a String using For Loop

/* C program to Toggle Case of all Characters in a String */
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str1[100];
  	int i;
 
  	printf("\n Please Enter any String to Toggle : ");
  	gets(Str1);
  	
  	for (i = 0; Str1[i]!='\0'; i++)
  	{
  		if(Str1[i] >= 'a' && Str1[i] <= 'z')
  		{
  			Str1[i] = Str1[i] - 32;
		}		
  		else if(Str1[i] >= 'A' && Str1[i] <= 'Z')
  		{
  			Str1[i] = Str1[i] + 32;
		}
  	}
  	printf("\n The Given String after Toggling Case of all Characters = %s", Str1);
  	
  	return 0;
}

The result of the above c program; as follows:

Please Enter any String to Toggle : hello
The Given String after Toggling Case of all Characters = HELLO

C program to Toggle Case of all Characters in a String using While Loop

/* C program to Toggle Case of all Characters in a String */
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str1[100];
  	int i;
 
  	printf("\n Please Enter any String to Toggle :  ");
  	gets(Str1);
  	
  	i = 0; 
  	while(Str1[i]!='\0')
  	{
  		if(Str1[i] >= 'a' && Str1[i] <= 'z')
  		{
  			Str1[i] = Str1[i] - 32;
		}		
  		else if(Str1[i] >= 'A' && Str1[i] <= 'Z')
  		{
  			Str1[i] = Str1[i] + 32;
		}
		i++;
  	}
  	printf("\n The Given String after Toggling Case of all Characters = %s", Str1);
  	
  	return 0;
}

The result of the above c program; as follows:

Please Enter any String to Toggle :  WORLD
The Given String after Toggling Case of all Characters = world

C program to Toggle Case of all Characters in a String using Ascii value

/* C program to Toggle Case of all Characters in a String */
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str1[100];
  	int i;
 
  	printf("\n Please Enter any String to Toggle :  ");
  	gets(Str1);
  	
  	for (i = 0; Str1[i]!='\0'; i++)
  	{
  		if(Str1[i] >= 65 && Str1[i] <= 90)
  		{
  			Str1[i] = Str1[i] + 32;
		}
		else if(Str1[i] >= 97 && Str1[i] <= 122)
  		{
  			Str1[i] = Str1[i] - 32;
		}
  	}
  	printf("\n The Given String after Toggling Case of all Characters = %s", Str1);
  	
  	return 0;
}

The result of the above c program; as follows:

Please Enter any String to Toggle :  c programming
The Given String after Toggling Case of all Characters = C PROGRAMMING

C program to Toggle Case of all Characters in a String using Function

/* C program to Toggle Case of all Characters in a String */
#include <stdio.h> 
void String_Toggle(char []);
 
int main()
{
	char str[100];
 
	printf("\n Please Enter any String to Toggle :  ");
	gets(str);
 
	String_Toggle(str);
 
	printf("\n The Given String after Toggling Case of all Characters = %s", str);
 
	return 0;
}
 
void String_Toggle(char Str1[]) 
{
	int i;
 
	for (i = 0; Str1[i]!='\0'; i++)
  	{
  		if(Str1[i] >= 65 && Str1[i] <= 90)
  		{
  			Str1[i] = Str1[i] + 32;
		}
		else if(Str1[i] >= 97 && Str1[i] <= 122)
  		{
  			Str1[i] = Str1[i] - 32;
		}
  	}
}

The output of the above c program; as follows:

Please Enter any String to Toggle :  functions
The Given String after Toggling Case of all Characters = FUNCTIONS

Be First to Comment

Leave a Reply

Your email address will not be published.