C Code for Coverting Upper to Lower Case and vice versa

Converting lower to upper and vice versa
/*
Easy program just taking care of the Ascii values and and there case.
The differnce between Uppercase character and Lowercase character is 32.
So if the character is in Uppercase we add 32 to change it to Lowercase and similarly we substract 32 from Lowercase character to make it Uppercase character.
*/
#include<stdio.h>
int main()
{
char string[30];
printf("Eneter a string:\n");
gets(string);
printf("u entered: %s\n",string);
char *str;
str=string;
while(*str!='\0')
{
if((*str>=65) && (*str<=91))
{
*str=*str+32;
str++;
}
else
if((*str>=97) && (*str<=123))
{
*str=*str-32;
str++;
}
else
{
printf("Not a character.\n");
break;
}
}
printf("Converted string is: %s\n",string);
return 0;
}
/*
#include<stdio.h>
int main()
{
char str[30];
printf("Eneter a string:\n");
gets(str);
printf("u entered: %s\n",str);
int i;
for(i=0;str[i]!='\0';i++)
{
if((str[i]>=65) && (str[i]<=91))
{
str[i]=str[i]+32;
}
else
if((str[i]>=97) && (str[i]<=123))
{
str[i]=str[i]-32;
}
else
{
printf("Not a character.\n");
break;
}
}
printf("Converted string is: %s\n",str);
return 0;
}
*/

No comments:

Post a Comment