Reversing the string without changing the Special character positions
Example :
i/p :
abcd#1234
o/p :
dcba#4321
Code:
import java.util.*;
class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String[] ch=str.split("\\W");
String[] sp=str.split("[a-zA-Z0-9]");
int i=0,len=0;
try{
for(String x:ch)
{
len+=x.length();
for(i=x.length()-1;i>=0;i--)
{
System.out.print(x.charAt(i));
}
System.out.print(sp[len]);
}
}
catch(Exception e)
{
}
}
}
Example :
i/p :
abcd#1234
o/p :
dcba#4321
Code:
import java.util.*;
class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String[] ch=str.split("\\W");
String[] sp=str.split("[a-zA-Z0-9]");
int i=0,len=0;
try{
for(String x:ch)
{
len+=x.length();
for(i=x.length()-1;i>=0;i--)
{
System.out.print(x.charAt(i));
}
System.out.print(sp[len]);
}
}
catch(Exception e)
{
}
}
}
No comments:
Post a Comment