Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java Write a function that takes an array of strings as an argument. Each string

ID: 3581263 • Letter: J

Question


java

Write a function that takes an array of strings as an argument. Each string in the array contains 3 fields separated by in the following format: string1, string2, index the function should process each string in the following manner: it should create a new string, which is composed of string2 into string1 at index for example, consider the following string. String s = "Helford, 10 W, J This means that index is 3, string2 is "10 W" and string1 is(I have placed the index of each character above the string)://Index: 0123456 string1 = "Helford" So the result of inserting "10 w" into "Helford" at index 3 is '"Hello World". Do this for each string in data, storing the result in the same position of your result array. For example, if the data array is: ("Helford, 10 w, 3". "Chilies con, 4", "CS310, E 13.2". "Jaa.v.2") your function would return the array containing: {"Hollo World", "Chris Conly", "CSE 1310", "Java") You can assume that each siring in in the proper format and that the index is a valid integer. You cannot assume how many strings are in the data array.

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