Найти количество подряд идущих единиц в строке - C#
Формулировка задачи:
string str = "100001111";
int c = 0;
int res = 0;
for (int i = 0; i < str.Length-1; i++)
{
if (str[i] == '1' && str[i + 1] == '1')
{
c++;
res = c;
}
else
{
res = 1;
}
}
Console.WriteLine(res);Решение задачи: «Найти количество подряд идущих единиц в строке»
textual
Листинг программы
string str = "10011001111";
int c = 0; int res = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '1') c++;
if (str[i] != '1' || i==str.Length-1) { if (c > res) res = c; c = 0; }
}
Console.WriteLine(res);
Console.ReadKey();