C Program to convert lowercase character to uppercase & vice versa


February 3, 2022, Learn eTutorial
1367

How to convert lower case to upper case in C?

The logic behind this c program is to read the sentence from the user and find out the length of the sentence by just traversing it. Put the length of the sentence into the variable count. After that display the user-given sentence. Then take each character of the string and check if it is uppercase or lowercase by using the function isupper() and islower().

If the character is in uppercase it will change to lowercase by calling the function tolower(). If the character is in lowercase it will change to uppercase by using toupper(). After the conversion, it will display the character by using the function putchar(ch). It will repeat the conversion and display the character until it reaches the end of the sentence.

What are islower() and isupper() functions for?

For checking whether a character is uppercase or lowercase we can use the built-in function of c islower() and isupper().

  • The islower() function will return true if the given character is in lowercase.
  • The isupper() function will return true if the given character is in uppercase.

What are toupper() and tolower() functions for?

  • To convert a character to uppercase we can use toupper()
  • To convert a character to lowercase we can use tolower()

Note: For using all functions we have to include the header file ctype.h.

ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Include the Header file ctype.h to perform the character testing like isupper(), islower().

STEP 3: Declare the Character Array Sentence[100] .

STEP 4: Accept the Sentence from the user to the Array sentence.

STEP 5: Find out the Length of the Sentence by just traversing through it by using a for loop

STEP 6: At the end of the Sentence add a Null character.

STEP 7: Display the Given Sentence.

STEP 8: By using a for loop and check each Character of the String is 'uppercase' or 'lowercase'. If the Character is Uppercase then change it into Lowercase and if the Character is in Lowercase change it into Uppercase by using the tolower(),toupper() functions respectively 

STEP 9: After the conversion, it displays each character of the string by using the function putchar(ch).


To convert the lowercase to uppercase characters we need to use the below C topics, we recommend learning the below topics for a better understanding

C Source Code

                                          #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++) /* enters the user sentence to change the case*/ {
    ;
  }
  sentence[i] = '\0'; /* add the null character at the end of sentence */
  count = i; /*shows the number of chars accepted in a sentence*/
  printf("The given sentence is   : %s", sentence);
  printf("\nCase changed sentence is: ");
  for (i = 0; i < count; i++) {
      ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]); /* check each letter of sentence using islower function if it is lowercase use toupper function to change it to uppercase else use tolower function to change uppercase to lowercase */
      putchar(ch);
  }
} /*End of main()*/
                                      

OUTPUT

Enter a sentence
World is Beautiful

The given sentence is: World is Beautiful
Case changed sentence is: wORLD IS bEAUTIFUL