Given the following header: vector split(string target, string delimiter); imple
ID: 3674880 • Letter: G
Question
Given the following header: vector split(string target, string delimiter); implement the function split so that it returns a vector of the strings in target that are separated by the string delimiter. For example: split("10,20,30", ",") should return a vector with the strings "10", "20", and "30". Similarly, split("do re mi fa so la ti do", " ") should return a vector with the strings "do", "re","mi", "fa", "so", "la", "ti", and "do". Write a program that inputs two strings and calls your function to split the first target string by the second delimiter string and prints the resulting vector all on line line with elements separated by commas. A successful program should be as below with variable inputs: Examples with inputs of : 10,20,30 and: do re mi fa so la ti do Enter string to split: 10,20,30 Enter delimiter string: , The substrings are: "10", "20", "30" Enter string to split: do re mi fa so la ti do Enter delimiter string: The substrings are: "do", "re", "mi", "fa", "so", "la", "ti", "do"
Explanation / Answer
class string_deli
{
public static String[] split(String target, String
delimiter)
{
String[] data=target.split(delimiter);
return data;
}
public static void main(String args[])
{
String s="10,20,30";
String d[]=split(s,",");
for(int i=0;i<d.length;i++)
System.out.print(d[i]+", ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.