Hi guys I need help on the code for this problem with using string. Can someone give me the answer to this problem and on how you do it with explanation but most improtantly the answer. Thank you

Method unscramble returns a version of String str with each pair of adjacent characters swapped. If the length of str is odd, the last character is left alone....

unscramble("eHllWorodl") → "HelloWorld"
unscramble("oCidgnsIuFn") → "CodingIsFun"
unscramble("nad") → "and"

the "answer" to this problem? you just gave a vague description, you mean you want us to do your homework for you ?
this looks like something fairly simple. why not write it yourself, and earn the points you want to get ?

Nobody is going to give you the answer. That destroys the whole point of giving you this to do as part of your education.

Here's some info that may help you do this yourself...
The String class has a method that converts to String into an array of characters
You can use ordinary array references to swap members of an array
The String class has a constructor that converts an array of characters back into a String

Now try to do this yourself. If you get stuck, post what you have done, and the harder you have tried, the more people will help.

any idea can be:

int semiLength = 2;
int pairs;

count your length of ur String / 2
String st = "HelloWorld"; =>pairs = st.length/semiLength; (result are int, and you have pairs - 5pairs for [He-ll-oW-or-ld])
after u can store it in an array :S
String [] pairsOfLetters = new String[pairs]
after u can store pairs in each portion of this array

for(int i= 0; i < pairs; i ++){
    //here the pairs can be saved 
    //(0-2)(2-4)(4-6)(6-8)(8-10)
    // He    ll   oW  or    ld
    pairsOfLetters[i] = st.subString(i*2,(i*2)+2) ;
}

after u can read or show it in a new string and use an permut method or reverse method (StringBuffer have an method - reverse())- adding it in an finalReverse String.

String finalReverse = "";
for(int j= 0; j < pairs; j++){
    String s = pairOfLetters[i];
    String finalReverse += new StringBuffer(s).reverse().toString();
}

They are only "ideas" i dont make it! ;) so, now u have the IDEA, now Try it! and Improve! then share the solution :D

and u can read: This link Click Here

Edited 2 Years Ago by zolymo

String finalReverse = "";
for(int j= 0; j < pairs; j++){
String s = pairOfLetters[i];
String finalReverse += new StringBuffer(s).reverse().toString();
}

not a very efficient way to do it. it's better to declare s and finalReverse out of the for loop.

Apart from small errors like the duplicate declaration, that approach can work, but it's horribly complicated and (if anyone cares) horribly inefficient. Convert to a char array and swap elements is much simpler (and faster)

zolymo, yes, we know, but the OP doesn't know enough about the language to spot the problems in your code, and you neglected to mention them.
you didn't just provide an idea, you provided code, which is basically copy-paste ready to finish a big part of his task.
it's better to mention possible downfalls before he hands it in with the thought "hey, look what brilliant code I wrote"

This article has been dead for over six months. Start a new discussion instead.