Recursively finding indices of a string

Hello

I'm trying to write a code that returns an Array whose elements are the indices of the word(occurences) that I'm looking for in a string : ex) d Input:

String sent = "Hi is Hi is Hi is";
String find = "Hi";

ArrayList<Integer> index = indexFinder(sent,find);
For(int i=0;i<index.size(),i++)
    System.out.println(index.get(i));

Output:

0
6
12

It would be very nice if Java had a string slicing function like python.. but since it does not.. I tried to use substring() method.

import java.util.ArrayList;

public class recursionEx {
ArrayList<Integer> index = new ArrayList<Integer>();
ArrayList<Integer> indexFinder(String sent, String find){
    int pos =0;
    int subPos =0;
    if(sent.contains(find)==false){
        return index;
    }
    else if(sent.contains(find)){
        pos = sent.indexOf(find);
        index.add(pos);
        subPos = pos+find.length();
        return indexFinder(sent.substring(subPos),find);
    }
    return index;
}

public static void main(String[] args) {
    String sent = "Hi is Hi is Hi is";
    String find = "Hi";
    recursionEx r = new recursionEx();
    ArrayList<Integer> g = r.indexFinder(sent, find);
    for(int i=0;i<g.size();i++){
        System.out.println(g.get(i));
    }

}

}

the output was

0
4
4

In hindsight, I'm gettng the substring of the original String sent every iteration, thus elements in index Array are the indices of the String find in substring of String sent which are 0,4,4.

How can I fix this to get the desired output? Any help is appreciated!

A. Ocannaille

You should not use the substring as parameter, but only the start of the search in the string. Here is a way that works, it is not perfect, try to make it better :

import java.util.ArrayList;

public class recursionEx {
ArrayList<Integer> index = new ArrayList<Integer>();
String string;
recursionEx(String string){this.string = string;}

ArrayList<Integer> indexFinder(int position, String find){
    int pos =0;
    int subPos =0;
    if(string.substring(position).contains(find)==false){
        return index;
    }
    else if(string.substring(position).contains(find)){
        pos = string.substring(position).indexOf(find) + position;
        index.add(pos);
        subPos = pos+find.length();
        return indexFinder(subPos,find);
    }
    return index;
}

public static void main(String[] args) {
    String sent = "Hi is Hi is Hi is";
    String find = "Hi";
    recursionEx r = new recursionEx(sent);
    ArrayList<Integer> g = r.indexFinder(0, find);
    for (Integer pos : g)
        System.out.println(pos);

}

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursively finding indices of a string

From Dev

Regex for finding a string recursively?

From Dev

Finding the max integer in an array, with specified start and ending indices in Java, **recursively**

From Dev

Finding a sub-string with indices from both ends

From Dev

Finding the number of matching letters in two different string at the same indices

From Dev

Finding the indices of all matches in a string, appending search term and indices to a dictionary.

From Dev

Recursively finding with AWK

From Dev

Finding largest file recursively

From Dev

Finding a substring recursively

From Dev

Finding empty directories recursively

From Dev

Finding indices of elements in vector

From Dev

Finding all indices by ismember

From Dev

Finding all indices by ismember

From Dev

Finding "islands" of territories on a map recursively

From Dev

Finding perimeter of a recursively modified square

From Dev

Finding "islands" of territories on a map recursively

From Dev

Finding sum of fibonacci numbers recursively

From Dev

Recursively finding combinations of ABC in Java

From Dev

Finding perimeter of a recursively modified square

From Dev

Recursively finding if an element belongs to a list

From Dev

finding specific indices with pointer array

From Dev

Finding indices of element in array ruby

From Dev

Finding Indices of Unique Elements in R

From Dev

Finding the column indices of submatrices in MATLAB

From Dev

recursively finding an element in 2d matrix

From Dev

Finding largest file size recursively from a directory

From Dev

Finding index of needle in haystack recursively.

From Dev

Finding a path using the Parents Array Recursively in Python

From Dev

Finding an object recursively, returns null when found

Related Related

  1. 1

    Recursively finding indices of a string

  2. 2

    Regex for finding a string recursively?

  3. 3

    Finding the max integer in an array, with specified start and ending indices in Java, **recursively**

  4. 4

    Finding a sub-string with indices from both ends

  5. 5

    Finding the number of matching letters in two different string at the same indices

  6. 6

    Finding the indices of all matches in a string, appending search term and indices to a dictionary.

  7. 7

    Recursively finding with AWK

  8. 8

    Finding largest file recursively

  9. 9

    Finding a substring recursively

  10. 10

    Finding empty directories recursively

  11. 11

    Finding indices of elements in vector

  12. 12

    Finding all indices by ismember

  13. 13

    Finding all indices by ismember

  14. 14

    Finding "islands" of territories on a map recursively

  15. 15

    Finding perimeter of a recursively modified square

  16. 16

    Finding "islands" of territories on a map recursively

  17. 17

    Finding sum of fibonacci numbers recursively

  18. 18

    Recursively finding combinations of ABC in Java

  19. 19

    Finding perimeter of a recursively modified square

  20. 20

    Recursively finding if an element belongs to a list

  21. 21

    finding specific indices with pointer array

  22. 22

    Finding indices of element in array ruby

  23. 23

    Finding Indices of Unique Elements in R

  24. 24

    Finding the column indices of submatrices in MATLAB

  25. 25

    recursively finding an element in 2d matrix

  26. 26

    Finding largest file size recursively from a directory

  27. 27

    Finding index of needle in haystack recursively.

  28. 28

    Finding a path using the Parents Array Recursively in Python

  29. 29

    Finding an object recursively, returns null when found

HotTag

Archive