Skip to main content

Java Program to accept a String and convert all lower case letters to upper case and upper case to lower

The following Code is to convert all the lower case letters to upper and upper case to lower. 'StringBuffer' Class is used because 'String' class is immutable but the 'StringBuffer' class is mutable. Which means that once the value stored in the 'String' type variable, it's value can't be changed but in 'StringBuffer' it can be changed.
Code:-

import java.util.*;
public class CaseConversion {
    int i;
    String string;
    char casse;
    String temp;
    Scanner scan = new Scanner(System.in);
    public void input() {
        try {
            System.out.println("Enter any String: ");
            string = scan.nextLine();
        } catch (Exception e) {
            System.out.println("Error Code: " + e);
            System.exit(0);
        }
    }
    public void compute() {
        StringBuffer conversion = new StringBuffer(string);
        for (i = 0; i < string.length(); i++) {
            casse = string.charAt(i);
            temp = Character.toString(casse);
            if (isUpperCase(casse)) {
                casse = temp.toLowerCase().charAt(0);
                conversion.setCharAt(i, casse);
            } else {
                casse = temp.toUpperCase().charAt(0);
                conversion.setCharAt(i, casse);
            }
        }
        System.out.println("\nString you entered: " + string);
        System.out.println("After Conversion: " + conversion);
    }
    public boolean isUpperCase(char tmp) {
        boolean upper = false;
        if (tmp >= 'A' && tmp <= 'Z') {
            upper = true;
        }
        return upper;
    }
    public static void main(String[] args) {
        CaseConversion obj = new CaseConversion();
        obj.input();
        obj.compute();
    }
}
Output:-
Enter any String:
ConverSION

String you entered: ConverSION
After Conversion: cONVERsion

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Vanilla Javascript each()

JQuery's each() is very useful when iterating through elements. But you don't want to use JQuery in your project you can simply add the following javascript code which works somewhat similar to the JQuery's each function. Here the fnc parameter is the function string which is converted to a valid function call replacing all the $(this) with this /**  * This function binds a particular function to every element with the specified selector. It is somewhat same as JQuery's each() with less functionality  * @param {String|DOMElement} selector  * @param {Function} fnc  */ function each(selector, fnc) {     var elem;     if (typeof selector === "string") {         elem = $_(selector);     } else {         elem = selector;     }     fnc = (fnc.toString().replace("$(this)", "elem") + "();").replace("function () {", "").replac...

Java Program to display Welcome Message

import java.io.*;// I/O package imported. public class Welcome{        //class name is "Welcome"     public Welcome(){      //constructor declaired to print the message.         System.out.println("Welcome to Java Programming Language!");/* System.out.println is used for output. Welcome Message is written within " ".*/     }//display() closes here.     public static void main(String[] args){        //main() is declaired to declair an object in it.         Welcome obj=new Welcome();  //Object "Obj" is bean created.     }//main() closes. }//class "Welcome" ends here. Above program displays the message which is written by you in " ".  In programs "/*" and "*/" are use for multiple line comment(s) and "//" is use for single line comment. Code line "Welc...