11 Dec 2017

write a program in java to reverse string that consists of special symbol ,reverse it without affecting the position of special symbole.


Given a string is :
input:     str="a,b$c"
output:  str="c,b$a"

input:     str="$b,c,d!e$"
output:  str="$e,d,c!b$"



class StringReverseWithoutAffectingSymbol
{
       public static void stringReverse(String input)
       {
              char ch[]=input.toCharArray();
              String revStr="";
              System.out.println("Given string is::"+input);
              for(int i=input.length()-1;i>=0;i--)
              {   if(Character.isLetter(ch[i]))
                     revStr+=ch[i];
              }
              char temp[]=revStr.toCharArray();
              int k=0;
              for(int j=0;j<ch.length;j++)
              {
                     if(Character.isLetter(ch[j]))
                     {
                           ch[j]=temp[k++];
                     }
              }
              System.out.println("Reversed string is::"+new String(ch));
              System.out.println("--------------------------------------");
              }
       public static void main(String[] args)
       {
              stringReverse("a,b$c");
              stringReverse("a!@bc&d");
       }
}


Output: