Определение какой из двух заданных символов встречается чаще в строке - C#
Формулировка задачи:
ПОМОГИТЕ ПЛИЗ!
Сделал чтобы выводились два символа но чот туплю, как сделать, чтобы выбралось которое чаще встречается из них
static void Main()
{
Console.WriteLine("Введите строку: ");
string a = Convert.ToString(Console.ReadLine());
Console.WriteLine("Введите символ для нахождения: ");
string b = Convert.ToString(Console.ReadLine());
Console.WriteLine("Введите второй символ для нахождения: ");
string c = Convert.ToString(Console.ReadLine());
int str1 = 0, index = 0, str2 = 0;
while ((index = a.IndexOf(b, index) + 1) != 0) str1++;
while ((index = a.IndexOf(c, index) + 1) != 0) str2++;
Console.WriteLine("Символ " + b + " встречается в тексте -> " + str1 + " raz(а)");
Console.WriteLine("Символ " + c + " встречается в тексте -> " + str2 + " raz(а)");
Console.ReadKey();
}
}
}Решение задачи: «Определение какой из двух заданных символов встречается чаще в строке»
textual
Листинг программы
using System;
class Program
{
static int Freq(string s, char a, char b)
{
int freq = 0;
foreach (char ch in s)
{
if (ch == a)
{
freq++;
}
else if (ch == b)
{
freq--;
}
}
return Math.Sign(freq);
}
static void Main()
{
switch (Freq("safdsgfafgafgan", 'f', 'a'))
{
case 1: Console.WriteLine("first"); break;
case -1: Console.WriteLine("second"); break;
default: Console.WriteLine("equal"); break;
}
}
}