I tried to write this Java code that alphabetically sorts a string without using arrays, but it fails on the first word of the input string. I've tried to fix it but I'm unable to figure out a solution. I'm learning to code and i need a hint.
This Java code implements a function that sorts an input string alphabetically without using arrays. The function is called sortWords and it takes three arguments: phrase, which is the string to be sorted; splitBy, which is the character or string of characters that will be used to split the string into individual words; and order, which is a character that determines whether the output order should be ascending ('<') or descending ('>').
sortWords uses several helper functions to accomplish its task. The countWords function counts the number of words in a given string, using the given character or string of characters as a delimiter. The getWord function retrieves a specific word from a given string, using the given character or string of characters as a delimiter. And the replaceWord function replaces a specific word in a given string with another given word, using the given character or string of characters as a delimiter.
The main logic of sortWords is as follows:
- Count the number of words in the given string using countWords.
- Iterate over all the words in the string, from the first to the last.
- For each word, search for the smallest word that comes before it in the string and has not been processed yet.
- If a smaller word is found, replace it with the original word using replaceWord, and save the original word for later use.
- Repeat this process until all words in the string have been processed.
- If a descending order ('>') has been specified, reverse the order of the words in the string using an auxiliary string and getWord.
- Return the sorted string.
Expected result: 'arbol,bedel,casa,diploma,excursion,factura,guea,xilofono'. Since 'f' comes before 'g' in the alphabet.
Observed result: 'arbol,bedel,casa,diploma,excursion,guea,factura,xilofono'. I think it fails because factura is the first word in the input string.
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
/* sort alphabetically this string without using arrays*/
String phrase;
phrase = "factura,casa,arbol,xilofono,bedel,diploma,excursion,guea";
System.out.println(sortWords(phrase, ",", '<'));
}
private static String getWord(String phrase, String splitBy, int number){
String word;
Scanner splitter;
int count;
splitter = new Scanner(phrase);
splitter.useDelimiter(splitBy);
word = "";
count = 1;
while (count <= number){
word = splitter.next();
count++;
}
return word;
}
private static int countWords(String phrase, String splitBy){
int count;
int position;
count = 0;
position = 0;
while(position != -1){
if (count == 0){
position = phrase.indexOf(splitBy);
} else {
position = phrase.indexOf(splitBy, position + 1);
}
if (position != -1){
count++;
}
}
return count + 1;
}
private static String sortWords(String phrase, String splitBy, char order){
int numberOfWords;
numberOfWords = countWords(phrase, splitBy);
for(int count = 1 ; count <= numberOfWords; count++){
String wordToCheck;
String wordToSwitchWith;
int switchWithNumber;
wordToCheck = getWord(phrase, splitBy, count);
wordToSwitchWith = wordToCheck;
switchWithNumber = count;
for(int checkVS = count -1 ; checkVS >= 1; checkVS--){
String wordToCompareWith;
wordToCompareWith = getWord(phrase, splitBy, checkVS);
if(wordToCheck.compareTo(wordToCompareWith) < 0 ){
wordToSwitchWith = wordToCompareWith;
switchWithNumber = checkVS;
}
}
phrase = replaceWord(phrase, splitBy, count, wordToSwitchWith);
phrase = replaceWord(phrase, splitBy, switchWithNumber, wordToCheck);
}
if (order == '>'){
String aux;
aux = "";
for(int count = numberOfWords ; count >= 1; count--){
aux += getWord(phrase, splitBy, count);
if(count != 1){
aux += splitBy;
}
}
phrase = aux;
}
return phrase;
}
private static String replaceWord(String phrase, String splitBy, int position, String replacement){
String output;
int numberOfWords;
output="";
numberOfWords = countWords(phrase, splitBy);
for(int count = 1 ; count <= numberOfWords; count++){
if(count != position){
output += getWord(phrase, splitBy, count);
} else {
output += replacement;
}
if(count != numberOfWords){
output += splitBy;
}
}
return output;
}
}