Program to Convert Lowercase to Uppercase of a String

Description

Two way To convert uppercase to lowercase or vice versa  
1) Travese the string character by character add 32 to convert uppercase to
   lowercase and substract 32 to convert lowercase to uppercase.
2) Use the language specific built in function.

C/C++

/* C program to convert a string lower to upper case */
//Save it as ConvertLowerToUpperCase.c


/*
  * The ASCII code of a character is stored in the memory not character itself.
  * ASCII values  of alphabets: A – Z is 65 to 90
  * ASCII values  of alphabets: a – z is 97 to 122
  * To convert lower case to upper case, substract 32 from the 
    ASCII value of the character
  * To convert upper case to lower case, add 32 to the 
    ASCII value of the character
*/

#include<stdio.h>
#include<ctype.h>
int main(){

    int i=0;
    char str[100];
    char upperCase[100];

    printf("Enter a string : ");
    gets(str);

    while(str[i]!='\0'){
        if(str[i]>='a' && str[i]<='z'){
            upperCase[i] = (str[i] - 32);
        }
        else{
            upperCase[i] = str[i];
        }
        i++;
    }
    upperCase[i]='\0';

    printf("The uppercase of %s is %s\n",str,upperCase);

    
    /*The library function toupper() and tolower() is used to convert a
      character into upper to lower respectively. */

    //It is defined in ctype.h

    int j = 0;
    char ch;

    printf("The uppercase of %s is ",str);
    while (str[j]) {
        ch = str[j];
        putchar(toupper(ch));
        j++;
    }

    return 0;
}
Input:
Enter a string : Virat Kohli

Output:
The uppercase of Virat Kohli is VIRAT KOHLI
The uppercase of Virat Kohli is VIRAT KOHLI

Java

/* Java program to convert a string lower to upper case */
//Save it as ConvertLowerToUpperCase.java


/*
 1) toLowerCase() convert a string to lowercase.
 2) toUpperCase() convert a string to uppercase.
*/


import java.io.*;
import java.util.Scanner;

public class ConvertLowerToUpperCase {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        String str;
        
        System.out.println("Enter a string : ");
        str = scanner.nextLine();
        
        System.out.println("The uppercase of "+str+" is "+str.toUpperCase());
    }
}
Input:
Enter a string : 
Virat Kohli

Output:
The uppercase of Virat Kohli is VIRAT KOHLI

Related Programs

1) Program to Convert UpperCase to LowerCase
2) Program to count uppercase and lowercase alphabet characters in String
3) Program to check String is Palindrome
4) Program to sort characters of strings in alphabetical order
5) Program to Sort set of strings in alphabetical order
6) Program to remove vowels from a String
7) Program to Multiply Numbers Present in a String
8) Program to remove given number from a string
9) Program to Replace Spaces With Dots
10) Find Occurrence Of Each Word In a Sentence
11) Print All Unique Words Of A String
Share Me

Leave a Reply