Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a new string with all occurrences of input character shifted to the end [closed]

Tags:

java

I have to return a new string with all the occurrences of input character shifted to the end. I tried to compare the given character by the string provided but not able to get the expected output.

public String moveChar(String str, char c) {
    int len=str.length();
    String newstr="";
    int m=len-1;
    for (int i=0; i<len; i++) {
        char ch1=str.charAt(i);
        char ch2=str.charAt(m);
        if (ch1==c) {
            newstr=newstr+ch2;
        }
        else {
            newstr=newstr+ch1;
        }
    }
    return newstr;
}

Expected Output: "heoll"

Actual Output: ""heooo"

like image 411
shilpi verma Avatar asked Dec 31 '22 17:12

shilpi verma


2 Answers

I recommend you start by getting the string as a char array using toCharArray(), manipulate the array, then turn it back into a string using new String(charArray).

You should manipulate the array by copying all characters that are not c to the next position. Sure, at first you're copying the chars to where they already are, but it's easier that way. When out of characters to copy, You know rest of the result has to be all c characters.

Here is a compact way to do it:

public static String moveChar(String str, char c) {
    char[] buf = str.toCharArray();
    for (int i = 0, j = 0; j < buf.length; i++)
        if (i >= buf.length)
            buf[j++] = c;       // fill
        else if (buf[i] != c)
            buf[j++] = buf[i];  // copy
    return new String(buf);
}

Illustrated, step by step, each iteration of the loop:

H e l l o     i = 0, j = 0
│             copy: buf[j++] = buf[i]; i++
↓
H e l l o     i = 1, j = 1
  │           copy: buf[j++] = buf[i]; i++
  ↓
H e l l o     i = 2, j = 2
              skip: i++

H e l l o     i = 3, j = 2
              skip: i++

H e l l o     i = 4, j = 2
    ┌───┘     copy: buf[j++] = buf[i]; i++
    ↓
H e o l o     i = 5, j = 3
              fill: buf[j++] = c; i++
      ↓
H e o l o     i = 6, j = 4
              fill: buf[j++] = c; i++
        ↓
H e o l l
like image 112
Andreas Avatar answered Jan 08 '23 01:01

Andreas


Instead of shifting the given character, you are eliminating it - you replace it with the last character.

You should count the number of characters you need to shift, and shift them at the end:

public String moveChar(String str, char c) {
    int len=str.length();
    String newstr="";
    int count=0;
    for (int i=0; i<len; i++) {
        char ch1=str.charAt(i);
        if (ch1==c) {
            count++;
        } else { // append only the characters that don't require shifting
            newstr=newstr+ch1;
        }
    }
    for (int i = 0; i < count; i++) { // append the characters that require shifting 
                                      // at the end
        newstr=newstr+c;
    }
    return newstr;
}

BTW, it would be better to use a StringBuilder instance to append the characters instead of using String concatenation that creates a new String instance.

public String moveChar(String str, char c) {
    int len=str.length();
    StringBuilder newstr=new StringBuilder(len);
    int count=0;
    for (int i=0; i<len; i++) {
        char ch1=str.charAt(i);
        if (ch1==c) {
            count++;
        } else { // append only the characters that don't require shifting
            newstr.append(ch1);
        }
    }
    for (int i = 0; i < count; i++) { // append the characters that require shifting 
                                      // at the end
        newstr.append(c);
    }
    return newstr.toString();
}
like image 41
Eran Avatar answered Jan 08 '23 01:01

Eran