Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ReverseStringWithoutSpecialCharacter {
- public String reverse(String input) {
- char temp = 0;
- char[] ch = input.toCharArray();
- for (int i = 0, j = input.length() - 1; i < j; ) {
- if (isAlphaNumeric(String.valueOf(ch[i])) &&
- isAlphaNumeric(String.valueOf(ch[j]))) {
- temp = ch[i];
- ch[i] = ch[j];
- ch[j] = temp;
- i++;
- j--;
- } else if (!isAlphaNumeric(String.valueOf(ch[i]))) {
- i++;
- } else if (!isAlphaNumeric(String.valueOf(ch[j]))) {
- j--;
- }
- }
- return String.valueOf(ch);
- }
- public boolean isAlphaNumeric(String s) {
- String pattern = "^[a-zA-Z0-9]*$";
- return s.matches(pattern);
- }
- public static void main(String[] args) {
- ReverseStringWithoutSpecialCharacter reverseStringWithoutSpecialCharacter = new ReverseStringWithoutSpecialCharacter();
- System.out.println(reverseStringWithoutSpecialCharacter.reverse("Basant@gmail.com"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement