Computer Science

Write a program in Java to enter a string in a mixed case. Arrange all the letters of string such that all the lower case characters are followed by the upper case characters.

Sample Input:
Computer Science

Sample Output:
omputercienceCS

Java

Java String Handling

25 Likes

Answer

import java.util.Scanner;

public class KboatStringLowerThenUpper
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter string: ");
        String str = in.nextLine();
        int len = str.length();
        StringBuffer sbLowerCase = new StringBuffer();
        StringBuffer sbUpperCase = new StringBuffer();
        
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            if (Character.isLowerCase(ch))
                sbLowerCase.append(ch);
            else if (Character.isUpperCase(ch))
                sbUpperCase.append(ch);
        }
        
        System.out.println("Input String:");
        System.out.println(str);
        String newStr = sbLowerCase.append(sbUpperCase).toString();
        System.out.println("Changed String:");
        System.out.print(newStr);
    }
}

Output

Answered By

7 Likes


Related Questions