Put chars from String into alfabetical order

M

Michael

Hi, Im stuck.
I want to have a method with:
- a parameter String s
- in the body it breaks the String s apart into chars then the chars
are put into alfabetical order.
- the return would give a String in alfabetical order.

In other words:
private String scramble(String s)
{
// put the chars from the string into alfatbetical order (black box)
return s;
}

Any suggestions?

Regards,

Michael (The Netherlands)
 
P

Patricia Shanahan

Michael said:
Hi, Im stuck.
I want to have a method with:
- a parameter String s
- in the body it breaks the String s apart into chars then the chars
are put into alfabetical order.
- the return would give a String in alfabetical order.

In other words:
private String scramble(String s)
{
// put the chars from the string into alfatbetical order (black box)
return s;
}

Any suggestions?

Regards,

Michael (The Netherlands)

Read the API documentation for each of these:

String method toCharArray. java.util.Arrays, String constructors.

Patricia
 
M

Michael

Thanks, these libraries are great :D
My solution:


private String scramble(String s)
{
int i;
String result ="";
char [] chararray;
chararray = s.toCharArray();
Arrays.sort(chararray);

for(i=0; i<s.length(); i++)
{
result+=chararray;
}
return result;
}
 
P

Patricia Shanahan

Michael said:
Thanks, these libraries are great :D
My solution:


private String scramble(String s)
{
int i;
String result ="";
char [] chararray;
chararray = s.toCharArray();
Arrays.sort(chararray);

for(i=0; i<s.length(); i++)
{
result+=chararray;
}
return result;
}


You can make this better by taking another look at the String constructors.

Patricia
 
G

Greg R. Broderick

Thanks, these libraries are great :D

Depending on what sort of course this program is for, your professor may want
you to implement your own sort rather than using a library implementation.


Cheers
GRB


--
---------------------------------------------------------------------
Greg R. Broderick [rot13] (e-mail address removed)

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top