This is a C program to replace lowercase characters by uppercase & vice-versa.
This program accepts the sentence and replaces lowercase characters by uppercase & vice-versa.
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.
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.
- /*
- * C program to read an English sentence and replace
- * lowercase characters by uppercase and vice-versa.
- * Output the given sentence as well as the converted
- * sentence on two different lines.
- */
- #include <stdio.h>
- #include <ctype.h>
- void main()
- {
- char sentence[100];
- int count, ch, i;
- printf("Enter a sentence \n");
- for (i = 0;(sentence[i] = getchar()) != '\n'; i++)
- {
- ;
- }
- sentence[i] = '\0';
- /* shows the number of chars accepted in a sentence */
- count = i;
- printf("The given sentence is : %s", sentence);
- printf("\n Case changed sentence is: ");
- for (i = 0; i < count; i++)
- {
- ch = islower(sentence[i])? toupper(sentence[i]) :
- tolower(sentence[i]);
- putchar(ch);
- }
- }
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.
Note: Join free Sanfoundry classes at Telegram or Youtube
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
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.
In this tutorial, we will see how to convert uppercase to lowercase in C. It is very easy to convert uppercase to lowercase in c.
Let’s say if you have any character in uppercase, you can simply convert it by just adding 32 to its ascii value.
In similar way, you can substract 32 from ascii character to convert lowercase to uppercase.
Let’s write complete program to convert uppercase to lowercase and vice versa.
printf("\n\nEnter The String: "); // Calculating length of input string if (str[i]>=65 && str[i]<=90) else if (str[i] >= 97 && str[i] <= 122) printf("\nConverted String(Lower/Upper) Is: %s\n",str); |
Output:
Enter The String: code2master
Converted String(Lower/Upper) Is: CODE2MASTER
Enter The String: CODE2MASTER
Converted String(Lower/Upper) Is: code2master
That’s all about convert uppercase to lowercase and vice versa.