How to write a C program to Toggle Case of all Characters in a String with example. To demonstrate the same we are going to use For Loop, While Loop, Functions, and ASCII Values
C program to Toggle Case of all Characters in a String using For Loop
This program allows the user to enter any string or character array. Next, it will use For Loop to iterate each character in that string. And converts the Lowercase character to uppercase, and uppercase character to lowercase.
We already explained the steps involved in Converting Lowercase to Uppercase and Upper case to Lowercase in two different articles.
#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;
}
C program to Toggle Characters Case in a String using ASCII Value
This program to toggle character in a string is the same as above, but this time we are using ASCII Table values inside the Else If Statement.
#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;
}
Please Enter any String to Toggle : TutoriAL GATEwaY
The Given String after Toggling Case of all Characters = tUTORIal gateWAy
C Program to Toggle Cases in a String Using While Loop
This program is the same as above, but this time we are using the While Loop.
#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;
}
Please Enter any String to Toggle : C PrograMMIng
The Given String after Toggling Case of all Characters = c pROGRAmmiNG
Program to Toggle Case of all Characters in a String Using Functions
This toggle character case in a string program uses the Functions concept to separate the logic from the main program.
#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;
}
}
}
Please Enter any String to Toggle : Learn C PROGRAMMING foR FreE
The Given String after Toggling Case of all Characters = lEARN c programming FOr fREe