0

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

4
  • Remove the chosen index character. Reverse the string and then re-insert the character at the position. Commented Oct 3, 2013 at 7:05
  • What is "the index choosen" in your example? Commented Oct 3, 2013 at 7:07
  • its 'C' .. and how to do it .. im quite blank in reversing string ... Commented Oct 3, 2013 at 7:09
  • Can you please post sample input and output examples.? Commented Oct 3, 2013 at 7:18

3 Answers 3

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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();
}

5 Comments

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.
This doesn't addresses his question. @Up voter Please read the question carefully and completely.
A slight modification - return string.subString(0,1)+new StringBuilder(string.subString(1)).reverse().toString();
@Jabir firstly thanks for care to comment, question it self is not clear enough, answer is given a better and shotter whay to achive a solution. Also check the comment trail, where it is clarified and answered.
@SubhrajyotiMajumder - You could edit your answer instead of posting the modified solution in comments. Answers and comments have their own specific purposes!:)
0

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.

1 Comment

This doesn't addresses his question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.