C program to toggle each character in a string

Toggle each character in a string.

In this article we will learn a C program to toggle each characters in a string i.e. to convert upper case characters to lower case characters and lower case characters to upper case characters in a string. This will help us to understand character conversion using ASCII values.

C program to toggle each character in a string

Algorithm:

  • Initialize the variables.
  • Accept the input.
  • Initiate a for loop.
  • Toggle each character.
  • Terminate the loop.
  • Print toggled string.
Competitive Coding Techniques

C programming code to toggle each character in a string.

#include <stdio.h>
#include <string.h>
 
int main()
{
    //Initializing variable.
  	char str[100];
  	int i;
  	
    //Accepting input.
  	printf("\n Please Enter any String: ");
  	gets(str);
  	
  	//Initializing for loop.
  	for (i = 0; str[i]!='\0'; i++)
  	{
  	    //Toggling characters.
  	    if(str[i] >= 'A' && str[i] <= 'Z') 
{
str[i] = str[i] + 32;
}
else if(str[i] >= 'a' && str[i] <= 'z') { str[i] = str[i] - 32; } } printf("\n Toggoled string: %s", str);//Print toggled string. return 0; }
Output:

Please Enter any String: PREPinsta 
Toggoled string: prepINSTA