-4

Working on a little java program currently, my task is to understand more on how the indexOf method works without actually using the indexOf method. I feel I am close having the code correct. I'll provide my code and my test file. I want to only use the charAt(), length(), and equals() methods for my program.

public class MiniString
{
   private String str;


   public MiniString(String x)
   {
      this.str = x;
   }


   public int findIndexOf(int a)
   {
      int counter = 0;
      for(int i = 0; i < this.str.length(); i++)
      {
         counter++;
      } 

      char[] arr = new char[counter];

      for(int i = 0; i < arr.length; i++)
      {
         arr[i] = this.str.charAt(i);
      }
   }
}

Here is what I am passing into my method

public class TestMiniString
{
   public static void main(String[] args)
   {

      MiniString s1 = new MiniString("Hello world, welcome to the Java world.");



      System.out.println(s1.findIndexOf('w'));
      System.out.println(s1.findIndexOf('m'));
      System.out.println(s1.findIndexOf('x'));
      System.out.println(s1.findIndexOf('J'));
      System.out.println(s1.findIndexOf('a'));
      System.out.println();

}
pro5476
  • 33
  • 1
  • 6

4 Answers4

1

It is quite simple. String in Java is a array of the chars internally. Using charAt(int index) you have direct access to this. All you have to is just loop this array and find first occurrence of the required character:

public final class MiniString {
    private final String str;

    public MiniString(String str) {
        this.str = str;
    }

    public int indexOf(int ch) {
        for (int pos = 0; pos < str.length(); pos++)
            if (str.charAt(pos) == ch)
                return pos;

        return -1;
    }

    public int indexOf(String str) {
        if (str == null || str.trim().isEmpty())
            return -1;

        for (int pos = 0; pos < this.str.length() - str.length(); pos++) {
            for (int i = 0; i <= str.length(); i++) {
                if (i == str.length())
                    return pos;
                if (this.str.charAt(pos + i) != str.charAt(i))
                    break;
            }
        }

        return -1;
    }
}

But be ware of exotic symbols, that is codded with more than one character (e.g. Chinese characters). So this is not universal solution for all situations.

Look at Java charAt used with characters that have two code units for more details.

indexOf(String str) implementation is strigtforward. Thare're many special algorithms for String searching algorithm.

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • Oleg thanks for your answer, what if we wanted to find the index of with lets says a String passed in as a parameter instead of an int, I am referring to the other method of indexOf. – pro5476 Feb 12 '18 at 20:32
0

Please try this. Hopefully, you like it.

    static int indexOf(String string, String substring ) {

    String crt = "";
    for (int i = 0; i < string.length(); i++) {

        if (string.charAt(i) == ' ')
            crt = "";
        else
            crt += string.charAt(i);
        if (crt.equalsIgnoreCase(substring))
            return i - crt.length() + 1;
    }
    return -1; 
    }
vkstream
  • 881
  • 8
  • 8
0
public static int subStringIndex(String str, String substr) {
        int strlen = str.length();
        int substrlen = substr.length();
       int count=0;
       int substrlen2=substr.length();
        if (substrlen >= 1) {
            for(int i=0;i<strlen-substrlen+1;i++){
                String A=str.substring(i,substrlen2);
                if(A.equalsIgnoreCase(substr)){
                return count+1;
                }else{
                    count++;
                    substrlen2++;
                }
            }
            return -1;              
        }
        return -1;
    }
0

Find the word index from main string also find which is main string and which is sub string

public static void main(String[] args) { 
   
    String string2="Today is a great day";
    String string1="great day";
    int a=string1.length();
    int b=string2.length();
    String [] p=string1.split(" ");
    String [] q=string2.split(" ");
    if(a>b)
    {
        for(int i=0;i<q.length;i++)
        {
            String x=q[i];         
            char d=x.charAt(0);
            for(int k=0;k<string1.length();k++)
            {
                if(string1.charAt(k)==d)
                {
                     if(string1.charAt(k-1)== ' ')
                 System.out.println(k);                     
                }               
            }
        }
    }
    else
    {         
        for(int j=0;j<p.length;j++)
        {
            String x=p[j];
            char d=x.charAt(0);
            for(int n=0;n<string2.length();n++)
            {
                if(string2.charAt(n)==d)
                {
                    if(string2.charAt(n-1)== ' ')
                System.out.println(n);
                }
            }
        }
    }    
}
Jaimil Patel
  • 1,301
  • 6
  • 13