Exercise 6 Write a search method named repeatedChars with tw

Exercise 6 Write a search method, named repeatedChars() with two parameters: a string s, and a positive int k The method should return a boolean indicating whether the same character is found in k adjacent positions in the string. Examples repeatedChars by\",3) returns true, because the character x is found in 3 adjacent places in the string. repeated Chars(\"vnhstxxxaby\'\',4) retums false. You may get inspiration from \"Linear search with more complicated matching

Solution

RepeatedCharsTest.java


public class RepeatedCharsTest {

  
   public static void main(String[] args) {
       System.out.println(repeatedChars(\"vnhstxxxaby\", 3));
       System.out.println(repeatedChars(\"vnhstxxxaby\", 4));
   }
   public static boolean repeatedChars(String s,int times){
       int count =1;
       for(int i=0; i<s.length()-1; i++){
           if(s.charAt(i) == s.charAt(i+1)){
               count++;
           }
           else{
               count = 1;
           }
           if(count == times){
               return true;
           }
       }
       return false;
   }
}

Output:

true
false

 Exercise 6 Write a search method, named repeatedChars() with two parameters: a string s, and a positive int k The method should return a boolean indicating whe

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site