title: Finding substring in a string in Java
body: <p>I am writing a program to find substring in the string in Java without using any Java Library.</p>
  
  <p>I had written a function <code>subString(String str1, String str2)</code> as shown below.</p>
  
  <p>It is working for the following input:</p>
  
  <ol>
  <li><code>str1="rahul" 
   str2="My name is rahul"</code></li>
  <li><code>str1="rahul"
   str2="rahul sah"</code></li>
  <li><code>str3="rahul" 
   str2="sah rahul"</code></li>
  </ol>
  
  <p>The problem occurs when I give input as:</p>
  
  <ol>
  <li><code>str1="rahul" 
   str2="rararahul"</code></li>
  <li><code>str1="rahul" 
   str2="My name is sunil"</code></li>
  </ol>
  
  <p>It goes to infinite loop. Can anyone have a look into my code snippet and help me out. </p>
  
  <pre><code>    public static boolean subString(String str1, String str2) {
          boolean found = false;
          int len1 = str1.length();
          int len2 = str2.length();
          int status = 0;
          char[] arr1 = new char[len1];
          char[] arr2 = new char[len2];
          for (int ii = 0; ii &lt; len1; ii++) {
              arr1[ii] = str1.charAt(ii);
          }
          for (int jj = 0; jj &lt; len2; jj++) {
              arr2[jj] = str2.charAt(jj);
          }
          for (int ii = 0; ii &lt; len1; ii++) {
              for (int jj = 0; jj &lt; len2; jj++) {
                  if (arr1[ii] == arr2[jj]) {
                      if (ii &lt; len1 - 1) {
                          System.out.println("Found1::" + "arr1::" + arr1[ii]
                                  + "and arr2::" + arr2[jj]);
                          found = true;
                          ii++;
                      } else if (arr1[ii] == arr2[jj] &amp;&amp; ii == len1 - 1) {
                          System.out.println("Found2::" + "arr1::" + arr1[ii]
                                  + "and arr2::" + arr2[jj]);
                          found = true;
                          break;
                      }
                  } else if (found == false &amp;&amp; arr1[ii] != arr2[jj]) {
                      System.out.println("Found3::" + "arr1::" + arr1[ii]
                              + "and arr2::" + arr2[jj]);
                      found = false;
  
                  } else if (found == true &amp;&amp; arr1[ii] != arr2[jj]) {
                      System.out.println("Found4::" + "arr1::" + arr1[ii]
                              + "and arr2::" + arr2[jj]);
                      found = false;
                      ii = 0;
                  }
              }
          }
          return found;
      }
  }
  </code></pre>