andrei.marfievici
BAN USERYou could use split:
public static String reverseWords(String input){
String output = "";
String[] inputList = splitWords(input," ");
for(int i = inputList.length-1; i>=0; i--)
output = output + inputList[i] + " ";
return output;
}
and the implementation of Split:
public static String reverseWords(String input){
String output = "";
String[] inputList = splitWords(input," ");
for(int i = inputList.length-1; i>=0; i--)
output = output + inputList[i] + " ";
return output;
}
You could use split:
public static String reverseWords(String input){
String output = "";
String[] inputList = splitWords(input," ");
for(int i = inputList.length-1; i>=0; i--)
output = output + inputList[i] + " ";
return output;
}
and the implementation of Split:
public static String reverseWords(String input){
String output = "";
String[] inputList = splitWords(input," ");
for(int i = inputList.length-1; i>=0; i--)
output = output + inputList[i] + " ";
return output;
}
Hmz, I didn`t really test it, I have to count the appearances of each char and validate.
- andrei.marfievici May 10, 2012
public static String[] splitWords(String s, String d){
- andrei.marfievici May 11, 2012int count = 0;
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i)==d.toCharArray()[0])
count++;
}
String[] output = new String[count+1];
int occurence = 0;
int first = 0;
int last = 0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)==d.toCharArray()[0] && occurence < count){
last = i;
output[occurence] = s.substring(first, last);
first = i + 1;
occurence++;
}
else if(occurence == count){
output[occurence] = s.substring(i,s.length());
break;
}
}
return output;
}