Friday, October 7, 2016

Toggle String

                                        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.
 

SymbolDecimal
A65
B66
C67
D68
E69
F70
G71
H72
I73
J74
K75
L76
M77
N78
O79
P80
Q81
R82
S83
T84
U85
V86
W87
X88
Y89
Z90


SymbolDecimal
a97
b98
c99
d100
e101
f102
g103
h104
i105
j106
k107
l108
m109
n110
o111
p112
q113
r114
s115
t116
u117
v118
w119
x120
y121
z122