Home > Software design >  How to flip even and odd elements in a string?
How to flip even and odd elements in a string?

Time:10-15

How do I flip every other word of a string? For example, for simplification, I want to turn: "Hello World Hello World Hello World" into "World Hello World Hello World Hello"

I've tried turning into a list, so that it would be easier to access each word, with: String[] temp = pattern.split(str);

and trying:

int oddInd = 1;
int evenInd = 0;
while (true) {
     while (evenInd < temp.length && temp[evenInd] % 2 == 0)
                evenInd  = 2;
     while (oddInd < temp.length && temp[oddInd] % 2 == 1)
                oddInd  = 2;
                 
     if (evenInd < temp.length && oddInd < temp.length) {
                int tt = arr[evenInd];
                temp[evenInd] = temp[oddInd];
                temp[oddInd] = tt;
     else
                break;

But it doesn't seem to work since I am dealing with strings and not integers. I also tried something simpler such as, but it also doesn't work.

for (int i = 1; i < temp.length; i =2) {
   if(i % 2 == 1) {
       temp[i] = temp[i-1];
            
   } else {
       temp[i] = temp[i 1];
  }
result = temp[i];

CodePudding user response:

You can just iterate over the even indices (increase by 2), then you know that every other index is odd:

String result = "";
for (int even = 0; even < temp.length; even  = 2) {
    int odd = even   1;
    if (odd < temp.length) {
        result  = temp[odd]   " ";
    }
    result  = temp[even]   " ";
}
result = result.trim();

CodePudding user response:

You can try the below approach with the logic as follows:

Approach Here:

I have spilt the given string via space and then iterate the created array with an index increment by 2 i.e i 2 and checking whether the i 1 is out of the range of the array or not. If not, then swapping the values else doing nothing on the array as shown below:

public class Test {
    public static void main(String[] args) {
        String s = "Hello World Hello World Hello World";

        String[] strArray = s.split(" ");//Creating a string array by splitting the given string with space

        for(int i = 0; i < strArray.length;i = i 2){//Iterating the string array and increasing the index by 2
            if(i 1 < strArray.length){// checking the value i 1 should be less than length of the array , do nothing if it fails
                //swapping Logic
                String temp1 = strArray[i];
                String temp2 = strArray[i 1];
                strArray[i 1] = temp1;
                strArray[i] = temp2;
                //swapping Logic
            }
        }
        for(String str: strArray){
            System.out.print(str   " ");
        }
    }
}

Output:

Input:: Hello World Hello World Hello World
Output:: World Hello World Hello World Hello

Input:: Hello World Hello World Hello
Output:: World Hello World Hello Hello

CodePudding user response:

Split then iterate by 2's swapping elements then join:

String [] words = str.split(" ");
for (int i = 0; i < words.length; i =2) { 
    String tmp = words[i];
    words[i] = words[i   1];
    words[j] = tmp;
}
String result = String.join(tmp, " ");
  • Related