2
votes

Given two strings str1 and str2 as input, I need to determine whether str2 occurs within str1 or not.

Examples:

  • Input #1: occurs("JavaLadders","Java")
  • Output #1: true
  • Input #2: occurs("Problem Panel","Panes")
  • Output #2: false

In my function I took 2 Strings. In for loop I am checking each character position whether that character in str1 matches in str2. If it does not match I made p=0 and count=0. If it matches I increment my count as well as p.

I also checked whether count is equal to my str2.length(). If it is, I move out of the loop and return true, otherwise I return false.

public boolean occurs(String str1, String str2)
{
    int l1=str1.length();
    int l2=str2.length();
    int p=0;
    int count=0;
    int j=0;
    for(;j<l1;j++)
    {
        char ch1=str1.charAt(j);
        char ch2=str2.charAt(p);
        if(ch1==ch2)
        {
            p++;
            count++;
        }
        else if(count==l2)
        {
            break;
        }
        else
        {
            p=0;
            count=0;
        }
    }
    if(l2==count)
    return true;
    else
    return false;
}

But this test case fails:

  • Input: occurs('Trisect Institute', 'Trisect')
  • Output: null
  • Expected output: true

What am I doing wrong?

6
Is there any constraint that forbids you to use str1.contains(str2) ?dounyy
@dounyy Yes,I cannot use any other function except concat and the functions I used.justin takro

6 Answers

1
votes

Your else if(count==l2) statement is in a wrong place. In your code, this condition does not check when ch1 == ch2. The following may help you:

public boolean occurs(String str1, String str2) {
    int l1=str1.length();
    int l2=str2.length();
    int p=0;
    int count=0;
    int j=0;
    for(;j<l1;j++) {
        char ch1=str1.charAt(j);
        char ch2=str2.charAt(p);
        if(ch1==ch2) {
            p++;
            count++;
        } else {
            p=0;
            count=0;
        }
        if(count==l2) {
            break;
        }
    }
    if(l2==count)
        return true;
    else
        return false;
}
0
votes
if(str1.contains(str2)){
    //str1 contains str2, do something here
} else {
    //str1 doesn't contain str2, do something different here
}
0
votes

The problem that you're facing is because you have the return statements inside the while statement. So the return is not always reachable.

You have to get this lines out of the while

if (count == str2.length()) {
    return true;
} else {

    return false;
}
0
votes

There are plenty of things wrong with your code. However, I recommend using one of the built-in string functions that Java provides, like String.indexOf() or String.contains().

  • If the first character doesn't match, you return either true or false.
  • You don't have a return statement in your method (it shouldn't compile)
  • You are checking on char index ranges and incrementing both of them if they are in the same range. This doesn't make sense. What are you trying to accomplish?
  • You can use String.toLower() if you are doing a case insensitive match, before you start comparing
  • If the first characters are out of range and different, it would skip checking altogether.
  • There are no comments which explain what you're trying to attempt

I would at least try and fix some of these and print out the results each iteration of the loop (or use a debugger) to help you.

0
votes

If you are not worried about building the functionality from the scratch use String.contains() method, else use KMP or boyre moore text searching algorithms, you can also try with brute force approach but it's not efficient

-2
votes
public boolean occurs(String str1, String str2) {
    //write your code here
    int len1=str1.length();
    int len2=str2.length();

    for(int i=0;i<(len1-len2+1);i++)
    {   
        String str="";
        for(int j=i;j<i+len2;j++)
        {
            str=str+str1.charAt(j);
            if(str.equals(str2)) return true;
        }
    }
    return false;
}