catDog string problem at Codingbat.com [closed]
- by stanny110
public boolean catDog(String str)
{ int catAnswer = 0;
int dogAnswer = 0;
int cat_Count = 0;
int dog_Count = 0;
for (int i=0; i< str.length()-1; i++)
{
String sub = str.substring(i, i+2);
if ((sub.equals("cat"))) cat_Count++;
if ((sub.equals("dog"))) dog_Count++;
catAnswer = cat_Count;
dogAnswer = dog_Count;
} //end for
if(dogAnswer == catAnswer ) {return true;}
// else
return (dogAnswer != catAnswer) ;