Posted by: Sourav | April 24, 2015

Sorting string converting string to array


class m{

public static void main(String args[])
{
int i,j;
String s=”sourav bhatta”;
String r=””;
char c;
char temp;
//int n=(int) s;//get ascii value
//char c=(char) 97; //ascii value to character

for(i=0;i<s.length();i++)
{
c=s.charAt(i);

for( j=i+1;j<s.length();j++)
{
c=s.charAt(i);

System.out.println(“i is =”+c);
System.out.println(“j is =”+s.charAt(j));
if((int) s.charAt(i)>(int) s.charAt(j))
{
System.out.println(“greater”);
c=s.charAt(j);

char[] chars = s.toCharArray();

temp=chars[i];
chars[i]=chars[j];
chars[j]=temp;
s = String.valueOf(chars);
System.out.println(“inside j s is = “+s);
}
}
r=r+c;
System.out.println(“inside i s is = “+s);
System.out.println(“r is = “+r);

}
System.out.println(r);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: