Tuesday, 27 August 2013

Character swapping

Character swapping

I'm trying to swap the first and last character in a string so what I did
was I was able to retrieve its first and last characters but am now having
hard times putting them all together:
String name="pera";
char[] c = name.toCharArray();
char first = c[0];
char last = c[c.length-1];
name.replace(first, last);
name.replace(last, first);
System.out.println(name);
Although I am getting for the variable 'first' the value of "p" and for
the variable 'last' the value of "a", these methods replace() are not
turning up with a valid result as the name stays as it is. Does anyone
have any idea on how to finish this?

No comments:

Post a Comment