Поставить запятые в текст - C#
Формулировка задачи:
В заданный текст после каждых трех слов поставить запятую. Распечатать текст на экран либо по 13 символов, если среди них нет запятой, либо до первой запятой включительно
Решение задачи: «Поставить запятые в текст»
textual
Листинг программы
class Program
{
static List<char> newString = new List<char>();
static void Main(string[] args)
{
{
string text = "One two three four five six seven eight nine ten eleven twelve";
int count = 0;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == ' ')
{
count++;
if (count % 3 == 0)
newString.Add(',');
}
newString.Add(text[i]);
}
Show();
Console.ReadKey();
}
}
static void Show()
{
int count = 0;
for (int i = 0; i < newString.Count; i++)
{
count++;
Console.Write(newString[i]);
if (newString[i] == ',' || count % 13 == 0)
{
Console.WriteLine();
count = 0;
}
}
}
}