Soccerdawg
Soccerdawg

Reputation: 11

Check a string to see if 2 letters are found next to each other in the string

I need to figure out how to determine true or false if 2 specific letters in a string are right next to each other.

For example: In AplusRunner the System.out.println(AB.check("frog","f","g")); should return false because the letters "f" and "g" are not right next to each other.

System.out.println(AB.check("chicken","c","k")); should return true since "c" and "k" are right next to each other.

All I need help with is how to determine if a string contains two letters right next to each other or not. Thanks

public class AB
{
    public static boolean check( String s, String a, String b)   
    {
   
    }
}
public class AplusRunner
{
    public static void main( String args[] )
    {


        System.out.println( AB.check( "chicken", "a", "b" ) );
        System.out.println( AB.check( "frog", "f", "g" ) );
        System.out.println( AB.check( "chicken ", "c", "k" ) );
        System.out.println( AB.check( "apluscompsci ", "a", "s" ) );
        System.out.println( AB.check( "apluscompsci ", "a", "p" ) );
        System.out.println( AB.check( "apluscompsci ", "s", "c" ) );
        System.out.println( AB.check( "apluscompsci ", "c", "p" ) );
            
    }
}

Upvotes: 0

Views: 1099

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521437

I would use String#contains here:

public static boolean check(String s, String a, String b) {
    return s.contains(a + b)
}

Upvotes: 3

Related Questions