Results 1 to 6 of 6
  1. #1
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default Swap chars in a String?

    Say I have a String that says "this is a test". I need to write code to swap each set of 2 chars, so for example, in this case, the swapped String would be "htsii s aetts". The 't' and 'h' in 'this' swap, the 'i' and 's' swap, the space and 'i' in 'is' swap, so on so forth. Can someone write code to do this? I can't seem to figure out how to do it..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,580
    Rep Power
    25

    Default

    Take a piece of paper and write down what you want to happen, step by step.
    When you have a process:
    Copy the characters in the String into a StringBuffer object.
    The StringBuffer class has methods for getting and setting its characters.
    Using these methods you can get and set the chars as required.
    When done, convert results to a String.

  3. #3
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    No one will write the code for you, but we'll certainly give you hints, or strategies.

    First I would put the string into a char array.
    Java Code:
    String str = "This is a test";
    char[] chArray = str.toCharArray();
    Than create a second char array
    char[] chArray2;
    Now try to write a loop statement (may be a few loops) to get the chArray into the chArray2 in your proper sequence.

    Hope this helps.
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by spatel14 View Post
    case, the swapped String would be "htsii s aetts". The 't' and 'h' in 'this' swap, the 'i' and 's' swap, the space and 'i' in 'is' swap, so on so forth.

    the method convertString above generate the following output

    htsii s aetts
    nao htrer nuw ti hnao htres rtnig

    Java Code:
    public class StringExample {
    
    	public static String convertString(String str) {
    		StringBuilder sb = new StringBuilder();
    		String s = str;
    
    		int i;
    		// i will be 0, 2, 4, 6
    		for (i = 0; i < s.length() / 2; i++) {
    			// print the i + 1 letter
    			sb.append(s.charAt(i * 2 + 1));
    			// print the i letter
    			sb.append(s.charAt(i * 2));
    		}
    		// if the length() is odd, add the last letter
    		if (i * 2 < s.length()) {
    			sb.append(s.charAt(i * 2));
    		}
    
    		return sb.toString();
    
    	}
    
    	public static void main(String[] args) {
    
    		System.out.println(convertString("this is a test"));
    		System.out.println(convertString("an other run with an other string"));
    
    	}
    }

    have fun.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,580
    Rep Power
    25

    Default

    Good work, now the OP won't have to bother with the hard bits about learning how to work thru a problem.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by j2me64 View Post
    the method convertString above generate the following output [snip]
    Why are you spoonfeeding? It is considered not done. The OP won't learn anything from it and with a bit of bad luck s/he'll cheat.

    kind regards,

    Jos

Similar Threads

  1. swap method
    By leavearth in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 06:59 PM
  2. Swap characters in a string.
    By Sdannenberg3 in forum New To Java
    Replies: 4
    Last Post: 03-04-2010, 08:49 PM
  3. Replacing the chars within a string.
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 03-27-2009, 04:00 AM
  4. How to print chars with symbols from a string
    By blacksky in forum New To Java
    Replies: 23
    Last Post: 01-06-2009, 01:14 PM
  5. how to swap 2 numbers
    By mary in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 05:32 PM

Posting Permissions

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