C program to Toggle Case of all Characters in a String

C program to toggle case of all characters in a string; Through this tutorial, we will learn how to toggle case of all or each characters in a string using for loop, while loop, ascii value, and function in c programs.

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

  • 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 output 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 output 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 output 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

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *