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.
Prerequisites.
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.
For Example:-
In the string “Hello”, there are five characters, they are as follows
Here,
‘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.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
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.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* C program to Toggle Case of all Characters in a String */ #include <stdio.h> #include <string.h> int main() { //declaring the required variable for the prorgam. 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; } |
Output:-
In the above program, we have first initialized the required variable.
- str1[100] = 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.