Results 1 to 3 of 3
  1. #1
    HelpMePls is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Unhappy Swapping pair of character in a string

    For one assignment I've gotten today, I need to take the string and swap every 2 pairs of characters as encryption.

    eg. testing here

    After the encrypting,

    It will be "ettsni geher"

    How do i go about it??

    Thanks a million in advance! :(

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Use the methods of the String class but remember String is immutable so you need to create a new String all the time.

    Or you can use StringBuilder or StringBuffer classes which are basically mutable Strings and have mostly the same methods that String has.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Another way would be to look at the String methods for one that gives you an array of characters. (And for a suitable constructor so you can get a String back when you have finished working on the array).

    Working along the array and swapping the characters is something that suggests using a for loop.

Similar Threads

  1. Removing last character from a String
    By newToJava3 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 12:43 AM
  2. compare String to the character .
    By Bulelakes in forum JDBC
    Replies: 12
    Last Post: 07-28-2010, 11:35 AM
  3. Taking a character off the end of a string.
    By AJArmstron@aol.com in forum New To Java
    Replies: 5
    Last Post: 05-05-2010, 02:56 AM
  4. how to get next character/string
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-28-2009, 04:04 AM
  5. How to store property file into key value pair
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-05-2008, 10:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •