Как найти число вхождения символа в строке
В этой статье мы напишем программу в C#, чтобы найти число вхождений символа в строке.
Использование для цикла
public class Program
{
public static void Main(string[] args)
{
string input = "csharpCS";
while (input.Length > 0)
{
Console.Write(input[0] + " : ");
int count = 0;
for (int j = 0; j < input.Length; j++)
{
if (input[0] == input[j])
{
count++;
}
}
Console.WriteLine(count);
input = input.Replace(input[0].ToString(), string.Empty);
}
Console.ReadLine();
}
}
Вывод:
c:2 s:2 h:1 a:1 r:1 p:1
