title: To check if a given string contains a substring without using contains or indexOf method
body: <p>In the following code the problem I am facing is how to make the inner loop start from a given index instead of 0 every time.</p>
  
  <p>LOGIC: </p>
  
  <pre><code>substr = rohan; 
  mainstr = herohanda
  </code></pre>
  
  <p>The code first check the first character of <code>substr</code> i.e <code>'r'</code> with every character of <code>mainstr</code> till it finds a match. When a match is found the program return to the outer loop and increments <code>i</code> to check if the 2nd charcater of <code>substr</code> (i.e <code>o</code>) matches the next character(the character next to the index where a match for <code>'r'</code> was found) of the <code>mainStr</code>. The problem is how to start the inner loop from that next character. Here it is starting with the initial index (0) each time  .</p>
  
  <p>CODE:</p>
  
  <pre><code>public class SubstringInString {
     public static void isSubstring(String subStr, String mainStr){
           int flag = 0;
           int counter = 0;
          OUTER: for(int i = flag; i&lt;subStr.length(); i = i+flag){
              INNER:  for(int j = 0; j&lt; mainStr.length(); j=counter ){
                          if(subStr.charAt(i) == mainStr.charAt(j)){
                              counter++;
                              flag++;
  
                              continue OUTER;
                          }
                          else
                          {
                              if((mainStr.length() - i) &gt;= subStr.length()){
                                  counter ++;
                                  flag = 0;
  
                                  continue INNER;
                              }
  
                              else{
                                  System.out.println("Main String does not contain the substring");
                              }
                          }
                      }
                  }
  
              //  System.out.println("Match found at " + j-subStr.length());
          }
  }
  </code></pre>
  
  <p>Please tell me what how to solve this and also if there is any better way to do the same.
  Thanks in advance </p>