Reputation: 21
I am confused about how to write a program that reverse the phrase but maintain the index chosen. Like this,
public static void main(String[] args) {
String string1 = "He is Chinese";
System.out.println(reverse(string1));
}
private static String reverse(String string) {
StringBuilder sb = new StringBuilder();
int length = string.length();
for(int i=0;i<length;i++) {
char a = string.charAt(i);
if(a == ' ') {
sb.append(a);
} else {
int j = i;
while(j < length && string.charAt(j) != ' ') {
j++;
}
sb.append(ReverseString(string.substring(i, j)));
i = j-1;
}
}
return sb.toString();
}
private static String ReverseString(String string) {
StringBuilder sb = new StringBuilder();
for(int i=string.length()-1;i>=0; i--) {
sb.append(string.charAt(i));
}
return sb.toString();
}
}
the choosen index is C. i want to keep the C in the places, but the other alphabet is reverse.
the output display is
eH si esenihC
Upvotes: 0
Views: 398
Reputation: 1062
Also if I don't well understand the phrase "maintain the index chosen" I believe that could be your snippets:
public static String reverse( String input ) {
String[] words = input.split(" ");
String ret = "";
for( String word : words) {
if( ret.length() > 0 ) { ret+=" "; }
ret += new StringBuilder(word).reverse();
}
return ret;
}
Hoping this may help you...Have fun!
Edit: if you want to keep a character in position after words reversing that could be a solution:
public static String reverse(String input, String pivot) {
String[] words = input.split(" ");
String ret = "";
for( int i = 0; i<words.length; i++ ) {
if (i != 0) { ret += " "; }
ret += reverse_word(words[i], pivot);
}
return ret;
}
public static String reverse_word(String input, String pivot) {
// warning: split will lose the last occurrence of pivot
String[] word_parts = input.split(pivot);
String ret = "";
for( int i = 0; i<word_parts.length; i++ ) {
if (i != 0) { ret += pivot; }
ret += new StringBuilder(word_parts[i]).reverse();
}
// check the last one
ret += input.endsWith(pivot) ? pivot : "";
return ret;
}
Upvotes: 2
Reputation: 234875
To avoid getting tangled up here with character indexes, I'd base a solution on the built-in reverse()
function in the StringBuilder
class, at the expense of performance.
Assume the preservation index is n
:
char c = string[n];
StringBuilder sb(string).deleteCharAt(n).reverse().insert(n, c);
return sb.toString();
Finally, this approach would generalise to an array of such preservation indices - use a loop calling deleteCharAt()
and a corresponding loop for reinsertion. Do let me know if you need that.
Upvotes: 0
Reputation: 41230
I really did not understand that maintain the index choosen, but if it is reversing string then,
How about it -
private static String reverse(String string) {
return new StringBuilder(string).reverse().toString();
}
Updation - As you said - and how to keep the 'C' in its own places? like Chinese, the other alphabet is reversing but only "C" is stayed put in its own places.
private static String reverse(String string) {
if(string==null||string.length()<=1)
return string;
return string.subString(0,1)+new StringBuilder(string.subString(1)).reverse().toString();
}
Upvotes: 1