Separator in String in java
You are given with a string along with delimiter such as ")" ,"(" , "}" ,"{" ,"[" , "]" should be replaced and saved as next element in array. Whereas delimiter is doubled "))" should be replaced with ")" of that respective delimiter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class SeperatorInStrings { | |
public static String[] separatersInString(String input1) | |
{ | |
String output1=""; | |
String output2=""; | |
String [] delimiter1={"(",")","{","}","[","]"}; | |
String [] delimiter2={"((","))","{{","}}","[[","]]"}; | |
for(int i=0;i<delimiter2.length;i++){ | |
String x=String.valueOf(delimiter2[i]); | |
for(int j=0;j<delimiter2.length;j++){ | |
if(input1.contains(String.valueOf(x))){ | |
output1=input1.replace(String.valueOf(delimiter2[j]), String.valueOf(i)); | |
input1=output1; | |
} | |
} | |
} | |
for(int k=0;k<delimiter1.length;k++){ | |
String y=String.valueOf(delimiter1[k]); | |
for(int l=0;l<delimiter1.length;l++){ | |
if(input1.contains(String.valueOf(y))){ | |
output2=input1.replace(String.valueOf(delimiter1[l]), " "); | |
input1=output2; | |
} | |
} | |
} | |
for(int f=0;f<delimiter1.length;f++){ | |
if(output2.contains(String.valueOf(f))){ | |
output1=output2.replace(String.valueOf(f), String.valueOf(delimiter1[f])); | |
output2=output1; | |
} | |
} | |
String [] outputArray=output2.split(" "); | |
return outputArray; | |
} | |
public static void main(String [] args){ | |
Scanner in = new Scanner(System.in); | |
String[] output = null; | |
String ip1 = in.nextLine().trim(); | |
output = separatersInString(ip1); | |
for(int output_i=0; output_i < output.length; output_i++) { | |
System.out.println(String.valueOf(output[output_i])); | |
} | |
in.close(); | |
} | |
} |
Input
abc(e))df)
Output:
abc
e)df
Input:
abc(efg)hij{lmn}
Output:
abc
efg
hij
lmn
abc(e))df)
Output:
abc
e)df
Input:
abc(efg)hij{lmn}
Output:
abc
efg
hij
lmn
Comments
Post a Comment