String Problem: Uppercase to Lowercase, Lowercase to Uppercase

Problem 70: Case Fix

You are given a string mixed with uppercase and lowercase letters . Your task is to write a program which will
flip the letters : (UpperCase -> LowerCase, LowerCase -> UpperCase)

Example:
INPUT:        OUTPUT:

eLepHanT   Elephant

cAt                  Cat

SuNDAY       Sunday

code:

not upto the output yet:

#include<stdio.h>
int main(){
int i,j;
char str[20];
gets(str);
for(i=0;str[i]!='\0';i++);

for(j=0;j<i;j++)
{
if(str[j]>='A' && str[j]<='z')
{
    if(str[j]>='A' && str[j]<='Z')
    {
    str[j]=str[j]+32;
    }
    else
    {
    str[j]=str[j]-32;
    }
}
//putchar(str[j]);
printf("%c",str[j]);
}
//

return 0;
}

beautiful code with exacts output:

#include<stdio.h>
int main()
{
    char str[200];
    int i,j;
    gets(str);
    for(i=0; str[i]!='\0'; i++);
    if(str[0]>='a' && str[0]<='z')
    {
        str[0]=str[0]-32;
    }
    putchar(str[0]);
    for(j=1; j<i; j++)
    {
        if(str[j]>='A' && str[j]<='Z')
        {
            str[j]=str[j]+32;
        }
        putchar(str[j]);
    }

    return 0;
}

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.