tags:

views:

101

answers:

3
String input = txtInput.getText();

char[] charArray =  input.toCharArray();
char[] flipArray = null;
System.out.println(charArray.length);
for (int i = 0; i < charArray.length ; i++) {
    System.out.println(charArray[i]);

sorry if the code doesn't make much sense.

charArray is taken from a JTextField .

So the code should do something like this.

  1. Takes in a message and flip every 2 characters. That is, the 1st and 2nd characters are switched and the 3rd and 4th are switched etc;
  2. For example, “You can't read my message!” will be “oY uac'n terdam yemssga!e” after encryption;

charArray would be the Message that says “You can't read my message!” flipArray would be carrying the message that says “oY uac'n terdam yemssga!e”

How do I write a loop that puts it in such that...

charArray[0] = flipArray[1]
charArray[1] = flipArray[0]
charArray[2] = flipArray[3]
charArray[3] = flipArray[2]
charArray[4] = flipArray[5]
charArray[5] = flipArray[4]

The value of charArray is taken from a JTextField.

I'm doing this on NetBeans IDE 6.5.1.

+2  A: 
char[] flipArray = new char[charArray.length];
...
    charArray[i] = flipArray[i^1];

Watch out for handling last element of odd numbered arrays.

^ is the exclusive-or operator.

Without bit-twiddling:

    charArray[i] = flipArray[(i/2)*2 + 1-(i%2)];

% is modulus operator (beware strange behaviour with negative numbers).

You could probably do it in a very clever way using NIO buffers, but I'm not going to try at this time of the day.

Tom Hawtin - tackline
Clever. In fact, too clever. I think that @Stefan's straightforward approach is more readable / maintainable.
Stephen C
+2  A: 

Tomfoolery.

for( int i = 0; i < charArray.length; i+= 2 )
{
   charArray[i] = flipArray[i+1];
   charArray[i+1] = flipArray[i];
}

I'm assuming that 0,1 goes to 1,0, 2,3 to 3,2, etc.

I think this is what you really mean:

String input = ...
StringBuilder builder = new StringBuilder();

for( int i = 0; i < input.length(); i += 2 )
{
   //guard against odd text lengths
   if( i+1 < input.length() )
   {
      builder.append( input.charAt(i+1) );
   }   
   builder.append( input.charAt(i) );
}

String flippedText = builder.toString();
Stefan Kendall
That would be the simpler way to go about it.
Tom Hawtin - tackline
i'm not a programming expert sorry. what happens if an odd number text is entered? what is append?
ProgrammingNub
+3  A: 

I'm going to step back to see the big picture and propose this solution to your problem instead:

System.out.println(
   "012345".replaceAll("(.)(.)", "$2$1")
);
// "103254"

That is, given a String s, s.replaceAll("(.)(.)", "$2$1")) returns a new string where every adjacent pairs of characters in s are swapped. If s has odd length, then the last character remains unswapped. If the string can contain newline characters, then use (?s) embedded Pattern.DOTALL.

Essentially the pattern is .. (i.e. "any two characters"), but each . is surrounded by brackets to create capturing groups so the captured match can be used in the replacement.

  MATCH: (.)(.)
          1  2
 REPLACE
   WITH:  $2$1

In Java regex, e.g. $1 in the replacement string refers to what group 1 captured in the match.

References


Variations

These are provided for more instructional values:

    System.out.println(
       "abcdefg".replaceAll("(.)(.)", "$1[$2]")
    );
    // "a[b]c[d]e[f]g"

    System.out.println(
       "> Regular expressions: now you have two problems!"
          .replaceAll("(.)(.)", "$2$1")
    );
    // " >eRugal rxerpseisno:sn woy uoh va ewt orpboelsm!"

    System.out.println(
       "> Regular expressions: now you have two problems!"
          .replaceAll("(\\w)(\\w)", "$2$1")
    );
    // "> eRugalr xerpseisnos: onw oyu ahev wto rpboelsm!"

    System.out.println(
       "Wow! Really?? That's awesome!!!"
          .replaceAll("(.)([!?]+)", "$1$1$1$2$2")
    );
    // "Wowww!! Reallyyy???? That's awesomeee!!!!!!"
polygenelubricants
Fist time I see a plastic surgeon swapping boobs with regex :)
Pascal Thivent
Regular expressions... is there nothing they can't do! :-)
Michael Rutherfurd