miro_ivanov

Longest Block in String

Apr 30th, 2021
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class LongestBlockInString {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. int currentCount = 1 ;
  9. int maxCount = 0 ;
  10. char maxChar = ' ';
  11. String string = scanner.nextLine();
  12. char[] chars = new char[string.length()];
  13. for(int i = 0 ; i < string.length(); i++) {
  14. chars[i] = string.charAt(i);
  15. }
  16. for (int i = 0 ; i < chars.length - 1; i++){
  17. if (chars[i] == chars[i+1]){
  18. currentCount++;
  19. }
  20. else currentCount = 1;
  21. if (currentCount > maxCount) {
  22. maxCount = currentCount;
  23. maxChar = chars[i];
  24. }
  25. }
  26. for (int i = 0 ; i < maxCount;i++) {
  27. System.out.print(maxChar);
  28. }
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment