In this tutorial you will learn about the C Program to Toggle Case of all Characters in a String and its application with practical example.
C Program to Toggle Case of all Characters in a String
In this tutorial, we will learn to create a C program that will Toggle the Case of all Characters in a String in C programming.
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Conditional Statements in C programming.
What is a String?
The String is a collection character. The character is a single entity in the English language or in any word. These characters can be any alphabets in lower case or upper case.
The lower case means the small letters. And the upper case means the capital letters of the English language.
In the string “Hello”, there are five characters, they are as follows
‘H’ is a character in the Upper case.
‘e’ is a character in the lower case.
‘l’ is a character in the lowercase.
‘l’ is a character in the lower case.
‘o’ is a character in the lowercase.
1. Declaring the variables for the program.
2. Taking the input String from the user.
3. Passing that variable to conditional statements.
4. Using those conditions to toggle the case of the String.
5. Printing the resultant String to the user.
6. End The Program.
Program to Toggle Case of all Characters in a String:-
First, we will take the input string from the user. Then will pass that String in the conditional statements to check and change the case of every character. It means the lower case character will be converted to the upper and upper case to lower.
With the help of this program, we can Toggle the Case of all Characters of the String.
/* C program to Toggle Case of all Characters in a String */
//declaring the required variable for the prorgam.
printf("\n Please Enter any String to Toggle : ");
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);
In the above program, we have first initialized the required variable.
- str1 = it will hold the String value for the input.
- i = it will hold the integer value for the loop.
Input String from the user.
Toggle all characters.
Printing toggled String.