Skip to content

Instantly share code, notes, and snippets.

@Younes-Charfaoui
Created December 28, 2017 12:24
Show Gist options
  • Save Younes-Charfaoui/7ce0422d192e498f0ac55967e34fa659 to your computer and use it in GitHub Desktop.
Save Younes-Charfaoui/7ce0422d192e498f0ac55967e34fa659 to your computer and use it in GitHub Desktop.
This is a solution for the Arrage the Strings problem
package Challegne;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ArrangeTheString {
private static List<String> word;
public static void main(String[] args) {
//scanner to read the input
Scanner inputScanner = new Scanner(System.in);
String input = inputScanner.nextLine();
word = getWordsFromString(input);
//getting the string and display it
String output = orderSentence();
System.out.println(output);
}
/**
* this function will tokenize the string into word
* and return them as a List
* @param input
* @return ArrayList
*/
private static ArrayList<String> getWordsFromString(String input) {
//trim the string
input = input.trim();
//getting reference to the start and the end of a world
int start = 0;
int end;
//the list that will be returned
ArrayList<String> returnList = new ArrayList<>();
//analysing the string
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == ' ') {
end = i;
returnList.add(input.substring(start, end));
start = i + 1;
}
if (i == input.length() - 1) {
returnList.add(input.substring(start, input.length()));
}
}
//and return the list
return returnList;
}
/**
* this function will create a String builder and sort the
* list from One to 9
* @return string
*/
private static String orderSentence() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < word.size(); i++) {
int j = 0;
while (!word.get(j).contains(String.valueOf(i + 1))) {
j++;
}
builder.append(word.get(j)).append(" ");
}
return builder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment