Write a program in Java to enter a string in mixed case. Arrange all the alphabets of the
string in such a way that all the lower case characters are followed by the upper case
characters.
Sample Input : Computer Science
Sample Output : omputercienceCS
Answers
Answer:
import java.util.*;
public class MixedCase
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string=");
String str = sc.nextLine();
char a; String str1="", str2="", str3="";
int i;
int l = str.length();
for(i=0;i<l;i++)
{
a=str.charAt(i);
if(Character.isUpperCase(a)==true)
{
str1+=a;
}
else if(Character.isLowerCase(a)==true)
{
str2+=a;
}
else
{
str3+=a;
}
}
System.out.println("Lower case characters followed by upper case=");
System.out.println(str2+""+str1);
}
}
Answer:
import java.util.*;
public class StringLowerThenUpper
{
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);
}
}
Explanation: