Monday, 25 December 2017

Reverse an string without affecting special characters in Java

Reverse an string without affecting special characters in Java,Reverse an string without affecting special characters,write a program to Reverse an string without affecting special characters

package com.dashzin.prog;
public class ReverseArrayWithoutSpecial
{
public static void main(String[] args)
{
System.out.println(reverseString("dash @zin#"));
}
public static String reverseString(String input)
{
char[] inputArr = input.toCharArray();
char[] tempArr = new char[input.length()];
int i=0;
int j=0;
for (char ch:inputArr)
{
if(Character.isAlphabetic(ch))
{
tempArr[i] = ch;
i++;
}
}
i--;
while(j<i)
{
char temp = tempArr[i];
tempArr[i]= tempArr[j];
tempArr[j]=temp;
j++;
i--;
}
for(i=0,j=0;i<input.length();i++)
{
if(Character.isAlphabetic(inputArr[i]))
{
inputArr[i]= tempArr[j++];
}
}
return new String(inputArr);
}
}

OutPut

nizh @sad#





DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

12 comments:

  1. I have read your blog its very attractive and impressive,Thanks for sharing

    Java Online Training

    ReplyDelete