Problem: The output should be reverse of a string input.
For example, Input: greatlearning;
Output: gninraeltaerg;
Input: to;
Output: ot;
public class reversingString {
public static void main(String[] args) {
// TODO Auto-generated method stub
String original = "greatlearning";
reversingString obj = new reversingString();
char [] get_result = obj.mirrorstring(original);
System.out.println(get_result);
}
public char[] mirrorstring(String original){
char [] chars = original.toCharArray();
for (int i=0,j=0; i<chars.length && j<chars.length/2; i++,j++){
char ch = chars[chars.length-1-j];
chars[chars.length-1-j] = chars[i];
chars[i]= ch;
}
return chars;
}
}
No comments:
Post a Comment