Java Experts need your help.
Today I was asked this questions in one of the interviews which I could not solve it. So I need a solution as to how should I solve this ;
Reversing a String
Input : Hello, World!
Output : olleH, dlroW!
In this case alphanumerics reverses and rest remains in the same place that means comma and Exclamation remains in their same place.
You can make use of only 4 String functions to get the answer;
- charAt(c)
- length()
- setCharAt(i,c)
- boolean isAlphaNumeric()
I tried the following code ;
public void String(String str){
String temp;
for(int i=str.length-1;i>=0;i--){
temp = temp + str.charAt(i);
}
}
But the above code reverses the whole string.