C Example | Convert LowerCase to UpperCase and vice-versa

This c programming example code will show us how to convert lowercase to uppercase and vice-versa.

LowerCase to UpperCase

#include <stdio.h>
#include <string.h>

int main() {
   char s[100];
   int i;
   printf("\nEnter a string : ");
   gets(s);

   for (i = 0; s[i]!='\0'; i++) {
      if(s[i] >= 'a' && s[i] <= 'z') {
         s[i] = s[i] - 32;
      }
   }
   printf("\nString in Upper Case = %s", s);
   return 0;
}

The output is:

Enter a string : hello world!
String in Upper Case = HELLO WORLD!

UpperCase to LowerCase

#include <stdio.h>
#include <string.h>

int main() {
   char s[100];
   int i;

   printf("\nEnter a string : ");
   gets(s);

   for (i = 0; s[i]!='\0'; i++) {
      if(s[i] >= 'A' && s[i] <= 'Z') {
         s[i] = s[i] + 32;
      }
   }

   printf("\nString in Lower Case = %s", s);
   return 0;
}

The output is:

Enter a string : HELLOWORLD
String in Lower Case = helloworld