I'm trying to use recursion to find the letters of a string in another String, so whenever the user inputs the letters to find it find the letters int the first string the user has inputted

computer science

Description

I'm trying to use recursion to find the letters of a string in another String, so whenever the user inputs the letters to find it find the letters int the first string the user has inputted

What I have below is so far that I got but the problem is that if I enter something like "The people are angry" and then I enter to find the letters "pope" it returns false so I don't know where it is going wrong

public static void main(String[] args)//Driver method
    {
        Scanner input = new Scanner(System.in);
        System.out.println("This Program will check if the letters of the string is found in another string ");
        System.out.println("Enter a sentence");
        String text = input.nextLine();
        System.out.println("Enter the letters to see if the letter are in the first sentece ");
        String check = input.next();



        System.out.println(containsInOrder(text, check));//Recursive method

    }

    public static boolean containsInOrder(String text, String check)
    {

        if (text.length() == check.length())
        {
            return text.equals(check);
        }
        else
        return text.startsWith(check) || containsInOrder(text.substring(1) , check);

    }

So I expect the output to be true when the letter pope is found in the string the people are angry and false otherwise


Related Questions in computer science category