garimaprasad1991
BAN USERpackage prac;
import java.util.ArrayList;
public class SortChar {
public static void Rearrange(ArrayList<Character> textArray)
{
LeftToRight(textArray);
RightToLeft(textArray);
}
private static void LeftToRight(ArrayList<Character> textArray)
{
int iPos;
for (iPos = 0; iPos < textArray.size(); iPos ++)
{
if (textArray.get(iPos).equals(' '))
{
int iLower;
// Swap this space with the first lower-case character after it.
for (iLower = iPos + 1; (iLower < textArray.size()); iLower ++){
if(Character.isLowerCase(textArray.get(iLower))){
char temp= textArray.get(iLower);
textArray.remove(iLower);
textArray.add(iPos, temp);
iPos++;
}
}
}
}
}
private static void RightToLeft(ArrayList<Character> textArray)
{
int iPos;
if (textArray.size() == 0) return;
for (iPos = (textArray.size() - 1); iPos >= 0; iPos --)
{
if (textArray.get(iPos).equals( ' '))
{
int iUpper;
// Swap this space with the first lower-case character after it.
for (iUpper = iPos -1; (iUpper >=0); iUpper --){
if(Character.isUpperCase(textArray.get(iUpper))){
char temp= textArray.get(iUpper);
textArray.remove(iUpper);
textArray.add(iPos, temp);
iPos--;
}
}
}
}
}
public static void main(String[] args)
{
String Text = "ab CDe fGHi JK lm";
ArrayList<Character> TextArray = new ArrayList<Character>();
for (int a=0; a<Text.length();a++){
TextArray.add(Text.charAt(a));
}
System.out.println(Text);
Rearrange(TextArray);
String t="";
for(int index=0; index< TextArray.size();index++){
t=t.concat(TextArray.get(index).toString());
}
System.out.println(t);
}
}
- garimaprasad1991 January 13, 2016