Convert Upper Case to Lower and Vice Versa in C



Converting upper to lower and lower to upper is generally termed as toggle.

Toggle each character means, in a given string, the lower letter is print in upper form and upper case is print in lower letter respectively.

Program

The C program to convert upper case to lower and lower case to upper is given below −

 Live Demo

#include <stdio.h>
#define MAX 100
void toggle(char * string);
int main(){
   char string[MAX];
   printf("enter the string need to be toggle :
");    gets(string);    toggle(string);    printf("final string after toggling is:
");    printf("%s
", string);    return 0; } void toggle(char * string){    int i=0;    while(string[i]!='\0'){       if(string[i] >= 'a' && string[i] <= 'z'){          string[i] = string[i] - 32;       }       else if(string[i] >= 'A' && string[i] <= 'Z'){          string[i]= string[i] + 32;       }       i++;    } }

Output

When you run the above mentioned program, you will get the following output −

enter the string need to be toggle :
TutoRialS PoinT C ProgrAmmIng LanGuage
final string after toggling is:
tUTOrIALs pOINt c pROGRaMMiNG lANgUAGE

Program

The C program to convert upper to lower and lower to upper by using the pre-defined function is as follows −

 Live Demo

#include <stdio.h>
int main(){
   int i, length = 0;
   char string[] = "TutORial";
   length = sizeof(string)/sizeof(string[0]);
   for(i = 0; i < length; i++){
      if(isupper(string[i])){
         string[i] = tolower(string[i]);
      }
      else if(islower(string[i])){
         string[i] = toupper(string[i]);
      }
   }
   printf("final string after conversion: %s", string);
   return 0;
}

Output

The output is as follows −

final string after conversion : tUTorIAL
Updated on: 2021-03-15T09:39:55+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements