title: Find the longest common prefix of two strings
body: <p>I want to find the longest common prefix of two strings.
  Is there a way to loop my last couple of if statements so that I can end at the last characters that do not match each other?</p>
  
  <pre><code>System.out.println("Enter the first string: ");
  String s = input.nextLine();
  
  System.out.println("Enter the second string: ");
  String s2 = input.nextLine();
  
  //check if first characters are same
  if (s.charAt(0) != s2.charAt(0)) {
    System.out.println(""+s+ " and "+s2+ " have no common prefix");
    System.exit(0);
      }
  
  if (s.charAt(0) == s2.charAt(0))
    System.out.print(" "+s.charAt(0));
  
  if (s.charAt(0) == s2.charAt(0))
    System.out.print(" "+s.charAt(1));
  
  if (s.charAt(0) == s2.charAt(0))
    System.out.print(" "+s.charAt(2));  
    }
  }
  </code></pre>
  
  <p>Example:</p>
  
  <pre><code>Enter first string: Welcome to c++
  
  Enter second string: Welcome to java
  </code></pre>
  
  <p>The code should return <strong>Welcome to</strong> as the common prefix.</p>