In a given string, which also contains special character along with alphabets (‘a’ to ‘z’ and ‘A’ to ‘Z’), reverse the string items in such a way that special character positions are not changed
Input: str = "x%y?z"
Output: str = "z%y?x"
Note that ? and % are not moved anywhere.
Only subsequence "xyz" is reversed
public class ReverseArrayWithoutSpecialChars {
public static void reverse(char[] str) {
int start=0;
int end= str.length-1;
for(;start<end;) {
if((str[start] >= 65 && str[start] <=90) || (str[start] >= 97 && str[start] <=122)) {
if((str[end] >= 65 && str[end] <=90) || (str[end] >= 97 && str[end] <=122)) {
char temp = str[start];
str[start]=str[end];
str[end]=temp;
start++;
end--;
}
else {
end--;
}
}
else {
start++;
}
}
}
public static void main(String[] args)
{
String str = "x%y?z";
char[] charArray = str.toCharArray();
System.out.println("Input string: " + str);
reverse(charArray);
String revStr = new String(charArray);
System.out.println("Output string: " + revStr);
}
}
.



