-1

This problem is related to String and array in Java. Say I have a String S= abcd. I have an array index[]={2}. My problem is to find out the value from String S at its index position 2 because array index[] hold 2. I have a target array which contain "EF". Now if S contain a at position 2 then it will replaced by "EF".

How to access 2 position of S. Is it something like that. S[index[0]] Is this return S[2]?

Example: Input: S = "abcd", indexes = [0,2], sources = ["a","cd"], targets = ["eee","ffff"] Output: "eeebffff" Explanation: "a" starts at index 0 in S, so it's replaced by "eee". "cd" starts at index 2 in S, so it's replaced by "ffff".

Saswati
  • 192
  • 8
  • Perhaps `S.charAt(2)` is what you need. You can review the API [here](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-). – dave Dec 31 '18 at 23:44
  • 1
    `S.charAt(index[0])` – shmosel Dec 31 '18 at 23:47
  • 1
    i did not get this `Now if S contain a at position 2 then it will replaced by "EF".`, Explain with clear example with sample input and output – Ryuzaki L Dec 31 '18 at 23:47
  • 1
    `S.toCharArray()[index[0]]` (save the `char[]` to a local variable if you need to access it more than once). – Elliott Frisch Jan 01 '19 at 00:03
  • @ElliottFrisch It throws some error :Error:(17, 21) java: method toCharArray in class java.lang.String cannot be applied to given types; required: no arguments found: int reason: actual and formal argument lists differ in length – Saswati Jan 01 '19 at 00:10
  • 1
    I said `S.toCharArray()[index[0]]`, **not** `S.toCharArray([index[0]])` - the first wasn't a typo. – Elliott Frisch Jan 01 '19 at 00:18

2 Answers2

1

Edited
After your comment, I added the sources array and assuming that both arrays sources and targets have the same length:

    String s = "abcd";
    String[] sources = {"a","cd"};
    int[] indexes  = {0, 2};
    String[] targets = {"eee", "ffff"};

    int more = 0;

    for (int i = 0; i < targets.length; i++) {
        int startIndex = more + indexes[i];
        int endIndex = more + indexes[i] + sources[i].length();
        if (startIndex < s.length() && endIndex <= s.length()) {
            String sBefore = s.substring(0, indexes[i] + more);
            String sAfter = s.substring(indexes[i] + sources[i].length() + more);
            if (sources[i].equals(s.substring(startIndex, endIndex))) {
                s = sBefore + targets[i] + sAfter;
                more += targets[i].length() - sources[i].length();
            }
        }
    }

    System.out.println(s);

will print

eeebffff
forpas
  • 160,666
  • 10
  • 38
  • 76
  • This code is ok. But `String [] sources` has no trace here. The ckecking should be done deepened upon source string. If the value of `sources` is equal to the value of String `S` at defined position then only String S position value is replaced by the sources values. So I try to modify the code ` String s = ""; for (String n:sources) s+= n;char[] c = s.toCharArray();for (int i = 0; i < array.length; i++) { if(array[indexes[counter]]==c[counter]){result.append(targets[counter]);if(counter<=indexes.length) { counter++; }else result.append(array[i]);}` **Array index outofBound error** – Saswati Jan 01 '19 at 02:14
1

My problem is to find out the value from String S at its index position 2

S.charAt(2) returns a char value at its index 2, or you can char[] charArray=S.toCharArray() to get a char[] and visit it in the array index way like charArray[2]

if S contain a at position 2 then it will replaced by "EF".

if(S.charAt(2)=='a'){
   StringBuilder sb=new StringBuilder(S);
   S=sb.replace(2,3,"EF").toString();
}

if you are sure that only one a can exists:

if(S.charAt(2)=='a'){
     S=S.replace("a","EF");
}
ZhaoGang
  • 4,491
  • 1
  • 27
  • 39