java Write a function that takes an array of strings as an argument. Each string
ID: 3581263 • Letter: J
Question
java
Explanation / Answer
private static String[] foo(String[] arr) {
String[] output = new String[arr.length] ;
for (int i = 0; i < arr.length; i++) {
String[] str = arr[i].split(",");
String str1 = str[0];
String str2 = str[1];
int index = Integer.parseInt(str[2]);
StringBuilder out = new StringBuilder();
out.append(str1.substring(0, index));
out.append(str2);
out.append(str1.substring(index, str1.length()));
output[i] = out.toString();
}
return output;
}
-------------------------------------------------------------------
full code:
public class StringConcat {
public static void main(String[] args) {
String[] arr = { "Helorld,lo W,3", "Chrily,s Con,4", "Jaa,v,2" };
String[] output = foo(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(output[i].toString());
}
}
private static String[] foo(String[] arr) {
String[] output = new String[arr.length];
for (int i = 0; i < arr.length; i++) {
String[] str = arr[i].split(",");
String str1 = str[0];
String str2 = str[1];
int index = Integer.parseInt(str[2]);
StringBuilder out = new StringBuilder();
out.append(str1.substring(0, index));
out.append(str2);
out.append(str1.substring(index, str1.length()));
output[i] = out.toString();
}
return output;
}
}
-----------------------------------
output:
Hello World
Chris Conly
Java
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.