How to display the alphabet of a word in ascending order.
Java stringDisplay the character of a word in ascending order
Page 1 of 1
12 Replies - 1713 Views - Last Post: 22 June 2009 - 03:47 PM
Replies To: Java string
#2
Re: Java string
Posted 21 June 2009 - 04:49 AM
write a program that will display all the words from a given string,the word should start and end wit a character 'a'.
#6
Re: Java string
Posted 21 June 2009 - 07:50 AM
well how about breaking it down using charAt( int index ); for each String and casting that to its integer form. for example
that way you can use integers to work out alphabetical order ? eg the char 'A' may have an integer value of 42 while 'D' may be 45 in integer form.
Note the output and gl with the program
class Test
{
public static void main(String[] args)
{
String p = "DCBA";
char v_c;
int v;
for (int i = 0; i < p.length(); i++)
{
v_c = p.charAt(i);
v = (int)v_c;
System.out.println(v_c + " = " + v + " if we take the int value of it");
}
}
}
that way you can use integers to work out alphabetical order ? eg the char 'A' may have an integer value of 42 while 'D' may be 45 in integer form.
Note the output and gl with the program
This post has been edited by bbq: 21 June 2009 - 07:57 AM
#7
Re: Java string
Posted 21 June 2009 - 05:44 PM
You can directly compare characters without converting them to ints or doubles b/c the char datatype is representative of base 256. So for example, the following is a legitimate boolean expression in Java.
'a' > 'b'
#8
Re: Java string
Posted 21 June 2009 - 06:36 PM
String.toCharArray()
is your best friend
is your best friend
#9
Re: Java string
Posted 21 June 2009 - 07:37 PM
macosxnerd101, on 21 Jun, 2009 - 04:44 PM, said:
You can directly compare characters without converting them to ints or doubles b/c the char datatype is representative of base 256. So for example, the following is a legitimate boolean expression in Java.
'a' > 'b'
Yep sure can however using their int values exemplifies the fact that they do have integer values - saying 'a'>'b' to a new comer could be a bit ambiguous. Also pbl's suggestion would work niceley
#10
Re: Java string
Posted 21 June 2009 - 09:59 PM
String name = cba;
char temp = ' ';
char[] word = name.toCharArray();
for(int i = 0; i<word.length;i++)
for(int x = 0; x<word.length-1;x++)
if(word[x] > word[x+1])
{
temp = word[x];
word[x] = word[x+1];
word[x] = temp;
}
Thats your classic sort. as pbl said, toCharArray is your best friend, to make it descending switch the condition to '<'
Happy Coding!
#11
Re: Java string
Posted 22 June 2009 - 02:45 PM
char[] digit = string.toCharArray();
Arrays.sort(digit);
Arrays.sort(digit);
#12
Re: Java string
Posted 22 June 2009 - 02:47 PM
ugh, owned by pbl again 
No matter what you come up with, he shall always come up with same thing that is easier and shorter!!
No matter what you come up with, he shall always come up with same thing that is easier and shorter!!
#13
Re: Java string
Posted 22 June 2009 - 03:47 PM
That's because I am lazy
The lazier you are the shortest code you produce
Why re-inventing the wheel when tools are there ?
The lazier you are the shortest code you produce
Why re-inventing the wheel when tools are there ?
This post has been edited by pbl: 22 June 2009 - 06:12 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|