Google Add

Search

C, C++ Program to Convert String from Uppercase to Lowercase

Write C, C++ Program to convert a string from uppercase to lowercase. How to solve this problem. We will solve this problem with and without using any in-built method.

Logic to Convert String from Uppercase to Lowercase

To convert whole string from uppercase to lowercase we need to take each character of a string and check whether it's in uppercase or lowercase.

i) Take a input string from user.

ii) Traverse each character of a string and check if their ASCII code is between 65 and 90.

ASCII code of A is 65 and Z is 90 (Uppercase character).
ASCII code of a is 97 and z is 122 (Lowercase character).

ASCII code refers to American standard code of information interchange. In ASCII each character is assigned a number.You can read more about ASCII on wikipedia.

Program to print ASCII value of input character.

Print ASCII value.

C, C++ Programming Questions.

We understand what is ASCII code. So to convert uppercase character to lowercase, we need to add 32 to uppercase so that it make lowercase.

Let's say input is

ABB

A = 65

65 + 32 = 97 (a)

B = 66

66 + 32 = 98 (b)

C Program to Convert String from Uppercase to Lowercase


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

int main(void) {
 
  char str[200];
  int len, i;
 
  printf("Enter a string (max 200 character) \n");
  gets(str);
 
  /* length of a string */
 
  len = strlen (str);

  for (i = 0; i<= len ; i++) {
  
    /* Check whether it's in uppercase */

    if (str[i] >= 65 && str[i] <= 90) {
   
       /* Convert it into lowercase */

       str[i] = str[i] + 32;
     }
  }
 
  printf ("Lowercase of a input string is %s",str);
 
  return 0;
}


Output :

Enter a string (max 200 character)   :  RAJ KUMAR

Lowercase of a input string is : raj kumar


C++ Program to Convert String from Uppercase to Lowercase


#include <iostream>
#include <string.h>
using namespace std;

int main() {
 
 char str[200];
 int len, i;
 
 cout << "Enter a string (Max 200 characters)\n";
 cin.getline (str, 200);
 
 len = strlen (str);

   for (i = 0; i<= len ; i++) {
  
    /* Check whether it's in uppercase */

    if (str[i] >= 65 && str[i] <= 90) {
   
        /* Convert it into lowercase */

        str[i] = str[i] + 32;
      }
    }
    
    cout <<" Lowercase string is " << str;
    
 return 0;
}

No comments:

Post a Comment