Toggle String
You have been given a String S consisting of uppercase and lowercase English alphabets. You need to change the case of each alphabet in this String. That is, all the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase. You need to then print the resultant String to output.Input Format
The first and only line of input contains the String S
Output Format
Print the resultant String on a single line.
SAMPLE INPUT:
abcdE
SAMPLE OUTPUT:
ABCDe
C code:
#include <stdio.h>
int main ()
{
int c = 0;
char ch, s[1000];
gets(s);
while (s[c] != '\0')
{
ch = s[c];
if (ch >= 'A' && ch <= 'Z')
s[c] = s[c] + 32;
else if (ch >= 'a' && ch <= 'z')
s[c] = s[c] - 32;
c++;
}
printf("%s\n", s);
return 0;
}
Explanation:
We know that for every alphabet there is an decimal value associated with it (List is available in the last of this post).We have used this concept to cinvert lower case into upper case and vice versa.
-First of all we done declared the values.
-Using gets function we can accept the string input while executing.
-After that we are comparing whether the position is the end of the string.
-We have applied logic that if the input character is in between A and Z then it is added with value 32 which converts it into lowercase and in the same way we compare whether the value is in between a to z and subtract 32 from it to convert it into uppercase.
Symbol | Decimal | |
A | 65 | |
B | 66 | |
C | 67 | |
D | 68 | |
E | 69 | |
F | 70 | |
G | 71 | |
H | 72 | |
I | 73 | |
J | 74 | |
K | 75 | |
L | 76 | |
M | 77 | |
N | 78 | |
O | 79 | |
P | 80 | |
Q | 81 | |
R | 82 | |
S | 83 | |
T | 84 | |
U | 85 | |
V | 86 | |
W | 87 | |
X | 88 | |
Y | 89 | |
Z | 90 |
|