Results 1 to 6 of 6
Thread: Swap chars in a String?
- 06-08-2010, 07:37 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 28
- Rep Power
- 0
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..
- 06-08-2010, 07:49 PM #2
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.
- 06-08-2010, 07:50 PM #3
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.
Now try to write a loop statement (may be a few loops) to get the chArray into the chArray2 in your proper sequence.Java Code:String str = "This is a test"; char[] chArray = str.toCharArray(); Than create a second char array char[] chArray2;
Hope this helps.:rolleyes: ~ Sno ~ :rolleyes:
'-~ B.S. Computer Science ~-'
- 06-08-2010, 08:59 PM #4
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.
- 06-08-2010, 09:03 PM #5
Good work, now the OP won't have to bother with the hard bits about learning how to work thru a problem.
- 06-08-2010, 09:05 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
swap method
By leavearth in forum New To JavaReplies: 1Last Post: 04-24-2010, 06:59 PM -
Swap characters in a string.
By Sdannenberg3 in forum New To JavaReplies: 4Last Post: 03-04-2010, 08:49 PM -
Replacing the chars within a string.
By Mayur in forum New To JavaReplies: 2Last Post: 03-27-2009, 04:00 AM -
How to print chars with symbols from a string
By blacksky in forum New To JavaReplies: 23Last Post: 01-06-2009, 01:14 PM -
how to swap 2 numbers
By mary in forum Advanced JavaReplies: 1Last Post: 08-02-2007, 05:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks