Advertisement
BSO90

Longest Block in String

Jul 1st, 2021
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Longest_block_In_string
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string text = Console.ReadLine();
  10.             int endIndex = 0;
  11.             int index = 0;
  12.             int count = 1;
  13.             int maxCount = 0;
  14.             int currIndex = 0;
  15.  
  16.             while (index != text.Length - 1)
  17.             {
  18.                 if (text[index] == text[index + 1])
  19.                 {
  20.                     currIndex = index + 1;
  21.                     count++;
  22.                    
  23.                 }
  24.                 else
  25.                 {
  26.                     count = 1;
  27.                 }
  28.  
  29.                 if (count > maxCount)
  30.                 {
  31.                     maxCount = count;
  32.                     endIndex = currIndex;
  33.                 }
  34.                 index++;
  35.                
  36.             }
  37.  
  38.             int startIndex = endIndex - maxCount + 1;
  39.             Console.WriteLine(text.Substring(startIndex, maxCount));
  40.         }
  41.     }
  42. }
  43.  
  44.  
  45.            
  46.            
  47.                    
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement