Three words on one line, separated by a space, are submitted to the input and they must be output in alphabetical order.
For example: input: Python, Java, C ; Output: C , Java, Python.
I get: P, J, C, C.
String sum = str1 " " str2 " " str3;
char ch1 = str1.charAt(0),
ch2 = str2.charAt(0),
ch3 = str3.charAt(0);
if (ch1 < ch2) {
}
System.out.println(ch1);
if (ch2 < ch3) {
System.out.println(ch1);
}
if (ch2 < ch1) {
}
System.out.println(ch2);
if (ch1 < ch3) {
System.out.println(ch2);
}
if (ch3 < ch1) {
}
System.out.println(ch3);
if (ch3 < ch2) {
}
System.out.println(ch3);
CodePudding user response:
Sorting based on only the first letter in each string is very fragile, but let's go with it.
You need to account for each possibility, and use else
.
To know which word to print first, we have to figure out which character is least.
if (ch1 < ch2 && ch1 < ch3) {
System.out.println(str1);
}
else if (ch2 < ch1 && ch2 < ch3) {
System.out.println(str2);
}
else {
// Only 1 option left.
System.out.println(str3);
}
You should be able to extrapolate from this, employing the correct boolean logic to determine which character comes second, and finally which comes third.
For instance, finding that the first word comes second:
if ((ch1 > ch3 && ch1 < ch2) || (ch1 > ch2 && ch1 < ch3)) {
System.out.println(str1);
}
To compare the entire strings, rather than just the first characters, you should definitely look into java.lang.String.compareTo
.
CodePudding user response:
Pretty simple. Using Java8:
package Test;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Testing {
public static void main(String[] args) {
String a = "dfhsjd", b = "sjsf", c = "tdfjg";
System.out.println(Stream.of(a, b, c)
.sorted()
.collect(Collectors.joining(" ")));
}
}
Adapt this code to fit your needs and you will be good to go.
CodePudding user response:
You could also break the strings into Characters and recognize that the ASCII values increase such that the value of 'a'
, 'b'
, and 'c'
are less than 'x'
, 'y'
, and 'z'
.
First, convert all the letters to a single case (upper or lower) because the value of 'A'
is less than 'z'
and the value of 'a'
is greater than 'Z'
Then, a simple test could give you something like this:
String first = "pYtHoN", second = "JAvA";
if(first.toUpperCase().charAt(0) < second.toUpperCase().charAt(0))
That tranlates to if('P' < 'J')
which is processed like if(80 < 74)
. From that, Java is able to identify that the letter J
alphetically comes before P
Bookmark this :D https://www.asciitable.com/
EDIT for more information
This is of course just a template for one way to approach this problem. If you pursue this approach, then continuing through the String
parameters will be a likely addition:
public static int position(String first, String second, int index)
{
char firstChar = first.toUpperCase().charAt(index),
secondChar = second.toUpperCase().charAt(index);
if(firstChar < secondChar)
return 1;
else if(firstChar > secondChar)
return -1;
else
return 0;
}
Then call that method in this loop, this is currently just loose code
String first = "pYtHoN", second = "JAvA";
// limit the Character reading to avoid IndexOutOfBoundsExceptions
int index = 0, limit = (
first.length() > second.length()) ? second.length : first.length();
boolean solved = false;
// increases index and verifies it hasn't been solved
while(index < limit && solved == false)
{
int position = position(first, second, index);
// ignored if it is the same letter
if(position != 0)
{
if(position == 1)
System.out.print(first " " second);
else
System.out.print(second " " first);
solved = true;
}
} // end of loop means either the order was found or they are the same
if(index == limit)
System.out.print("Same word");
In case this syntax limit = (first.length() > second.length()) ? second.length : first.length();
is new, it is basically a compact way of writing an if-else
statement. So:
datatype variable = (test) ? thisVal : thatVal;
// is essentially
datatype variable = new datatype();
if(test)
variable = thisVal;
else
variable = thatVal;