BorislavBorisov

Редици.05.01.Най-дългата дума в стринг

Oct 31st, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1. using System;
  2. class LongestDecrisingSubs
  3. {
  4.     static void Main()
  5.     {
  6.         string str = "asd dfсд sdfd";
  7.         findLongestWord(str);
  8.     }
  9.  
  10.     static void findLongestWord(string str)
  11.     {
  12.         int len = 0, bestLen = 0, endIndex = 0;
  13.  
  14.         for (int i = 0; i < str.Length; i++)//дължината не е минус едно зашото бройме
  15.         {
  16.             if(str[i] != ' ')
  17.             {
  18.                 len++;
  19.                 if(len > bestLen)//ако има две равни и if-a е равно дава най дясната, за най-дългата
  20.                 {
  21.                     bestLen = len;
  22.                     endIndex = i;
  23.                 }
  24.             }
  25.             else
  26.             {
  27.                 len = 0;
  28.             }
  29.         }
  30.         Console.Write("Longest word: ");
  31.         for (int i = endIndex - bestLen + 1; i <= endIndex; i++)
  32.         {
  33.             Console.Write(str[i]);
  34.         }
  35.         Console.WriteLine();
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment