-1
public class CharMatch {
    private void matchString(String s1, String s2) {
        int count = 0;
        int len1 = s1.length();
        int len2 = s2.length();
        for (int i = 0; i < len2; i++) {
           for (int j = 0; j < len1; j++) {
              if (s2.charAt(i) == s1.charAt(j)) {
                  count++;
              }
              if (count == len2) {
                  System.out.print("True");}
              }
          }
     }     

   public static void main(String args[]) throws IOException {
      CharMatch cm = new CharMatch();
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter First String");
      String a = br.readLine();
      System.out.println("Enter Second String");
      String b= br.readLine();
      cm.matchString(a,b);
   }
}

I was writing this code to input two string for example input: s1="Hello world" s2:"woeh" then the output should be true if all the characters in s2 matches at least once in s1. This is the code which I wrote, but I am not getting at perfect output as desired.

Gray
  • 115,027
  • 24
  • 293
  • 354

3 Answers3

1

This is the code which I wrote, but I am not getting at perfect output as desired.

There are a couple of problems with your code.

  • As @MacRyze mentions, the problem with matching "woeh" in the characters of "Hello world" is that you need to lowercase your strings if you want 'h' to match with the 'H' in case insensitive manner.
  • Another big problem is that you will count characters like 'o' twice in the first string which I don't think you want to do. You should put a break after the count++; once you find the first character match. Also, you should test for if (count == len2) { in the same block as the count++ and you should then probably return from the method if it is true. You could also do that chect at the end of the method.

Ultimately, you should have been able to find this bug if you used a debugger and stepped through the example. Here's a good tutorial for eclipse.

I've refined the code a bit and used the String.indexOf(...) method and arrived at:

private void matchString(String s1, String s2) {
    int matchCount = 0;
    s1 = s1.toLowerCase();
    s2 = s2.toLowerCase();
    // go through all characters in the match string
    for (char ch : s2.toCharArray()) {
        // look for the character in the first string
        if (s1.indexOf(ch) >= 0) {
            matchCount++;
        }
    }
    if (matchCount == s2.length()) {
        System.out.print("True");
    }
}
Gray
  • 115,027
  • 24
  • 293
  • 354
0

I guess it is really important if character case matters. If it's not I guess this little code should do the work:

        s1 = s1.toLowerCase();
        s2 = s2.toLowerCase();
        boolean flag = true;
        for(char element : s2.toCharArray()){
           if(!s1.contains(Character.toString(element))){
               flag = false;
           }
        }
        System.out.println(flag);

On the other hand if character case matters just remove s1 = s1.toLowerCase(); and s2 = s2.toLowerCase();. good luck with your code!

Macryo
  • 188
  • 2
  • 12
  • Creating a string of a character so you can do a contains is pretty expensive. Using String.indexof(ch) works with the characters instead. – Gray Nov 20 '16 at 07:49
  • Yeah, You are right Gray, your version seems way better than mine. Thanks for comment, still I have much to learn! – Macryo Nov 20 '16 at 12:34
-1

Use the java string contains() method searches the sequence of characters in this string. It returns true if sequence of char values are found in this string otherwise returns false.

void matchString(String s1,String s2)
{
    if(s2.contains(s1))
       System.out.print("True");
   else
      System.out.print("False");

}
Harish Nayak
  • 348
  • 3
  • 18