C Program to Convert Lowercase Characters by Uppercase and Vice-Versa

This is a C program to replace lowercase characters by uppercase & vice-versa.

Problem Description

This program accepts the sentence and replaces lowercase characters by uppercase & vice-versa.

Problem Solution

1. Take the sentence as input.
2. Using (islower()? toupper():tolower()) function replace lowercase characters by uppercase & vice-versa.
3. Print the output and exit.

Program/Source Code

Here is source code of the C program to replace lowercase characters by uppercase & vice-versa. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to read an English sentence and replace
  3.  * lowercase characters by uppercase and vice-versa.
  4.  * Output the given sentence as well as the converted
  5.  * sentence on two different lines.
  6.  */
  7. #include <stdio.h>
  8. #include <ctype.h>
  9.  
  10. void main()
  11. {
  12.     char sentence[100];
  13.     int count, ch, i;
  14.  
  15.     printf("Enter a sentence \n");
  16.     for (i = 0;(sentence[i] = getchar()) != '\n'; i++)
  17.     {
  18.         ;
  19.     }
  20.     sentence[i] = '\0';
  21.     /*  shows the number of chars accepted in a sentence */
  22.     count = i;
  23.     printf("The given sentence is   : %s", sentence);
  24.     printf("\n Case changed sentence is: ");
  25.     for (i = 0; i < count; i++)
  26.     {
  27.         ch = islower(sentence[i])? toupper(sentence[i]) :
  28. tolower(sentence[i]);
  29.         putchar(ch);
  30.     }
  31. }
Program Explanation

1. Take an an English sentence as input and store it in the array sentence[].
2. Copy the last letter’s position in the array to the variable count.
3. Using for loop and (islower()? toupper():tolower()) function replace lowercase characters by uppercase & vice-versa. Store this in the variable ch.
4. Print the variable ch as output and exit.

advertisement
advertisement
Runtime Test Cases
Enter a sentence
wELCOME tO sANFOUNDRY
The given sentence is   : wELCOME tO sANFOUNDRY
Case changed sentence is: Welcome To Sanfoundry

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.