0

Hi I want to swap the numbers in string form in specific way.

For example :

If i have 10325476 then it should return 01234567 whatever i had tried so far is i just swapped the the pair of two numbers in String.

EDIT
if i have 01AB3423 then Answer should be 10BA4332 Following is my current implementation which gives me desired output.

char temp1 = c1[0];
c1[0] = c1[1];
c1[1] = temp1;

char temp2=c1[2];
c1[2]=c1[3];
c1[3]=temp2;

char temp3=c1[4];
c1[4]=c1[5];
c1[5]=temp3;

char temp4=c1[6];
c1[6]=c1[7];
c1[7]=temp4;

I have done this for all 8 numbers but its a static way. And i know its not a correct way to do this.I want to do this dynamically as well.is there any other way to implement this?

Any idea and advice will be appreciated.

Juned
  • 6,290
  • 7
  • 45
  • 93

2 Answers2

3

Edit: -

From your latest edit, it seems like you want to swap consecutive pairs of characters in your string. You would need a loop in that case.

Try this code: -

String str = "BA34CD567";
char[] arr = str.toCharArray();

for (int i = 0; i < arr.length - 1; i += 2) {
    char temp = arr[i];
    arr[i] = arr[i + 1];
    arr[i + 1] = temp;
}

System.out.println(arr);

Output : -

AB43DC657

I have incremented i by 2 in the loop, because you are considering two elements on each iteration.


Old Answer : -

Just 3 steps: -

  • Convert your string to char array. (str.toCharArray())
  • Sort your char array. (Arrays.sort(arr)). Remember, Arrays.sort does not return the sorted array. It sorts the array in place.
  • And then print your array. (Well, you know how to do this)
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Thanks for response,i didn't get this line `Arrays.sort does not return the sorted array. It sorts the array in place` can explain it in detail? – Juned Dec 02 '12 at 15:10
  • @RohitJain You beat me to it! Nice answer without exactly coding it for them. – xagyg Dec 02 '12 at 15:10
  • @juned.. When you do `Arrays.sort` you don't need to store it the result in an array. It will aumatically sort your array. Just print it after you used `Array.sort`. – Rohit Jain Dec 02 '12 at 15:13
  • @juned.. xagyg has given an implementation for the algorithm I posted. Now you have both. You can understand it easily now. – Rohit Jain Dec 02 '12 at 15:16
  • @juned. So, you really wanted to sort and not swap every number pair. What output do you want for - `4352678`? – Rohit Jain Dec 02 '12 at 15:21
  • HI @RohitJain i have some problem with this its doesn't give the desired output, if i have `BABABABA` then it should return `ABABABAB` but with this it returns `AAAABBBB` for `4352678` it should give `34257608` – Juned Dec 02 '12 at 17:38
  • @juned.. That is why I asked for clarificatin that you want sorting or swapping. See my last comment in this question. – Rohit Jain Dec 02 '12 at 17:57
  • @RohitJain yeah my mistake, now it seems absolutely correct. Thanks Again – Juned Dec 02 '12 at 18:07
  • @RohitJain i need you some help in this question http://stackoverflow.com/questions/13721637/method-to-pass-a-only-two-element-of-byte-array – Juned Dec 05 '12 at 11:02
0

Take a look at some sorting algorithms, such as bubble sort (if it is a small set of numbers), merge sort, quicksort, and others. This is what you seem to want to do -- sorting.

Victor Zamanian
  • 3,100
  • 24
  • 31