Ребят, помогите плиз! Есть string s1 = "Michael have 20 fingers."; Нужно создать отдельные функции(алгоритмы) для: 1. Подсчет слов в s1 (3 слова) 2. Первое слово в s1(Michael) 3. Количество разных букв, которые не повторяются(13) Заранее спасибо!
Ну если очень просто, то вот так: using System; using System.Linq; namespace test2 { class Program { static void Main() { string s1 = "Michael have 20 fingers."; int count = 0; string[] s1a = s1.Split(' '); foreach (string q in s1a) { if (!int.TryParse(q, out int qq)) count++; } s1 = s1.Replace(" ", "").Replace(".", "").Replace("0", "").Replace("1", "").Replace("2", "").Replace("3", "") .Replace("4", "").Replace("5", "").Replace("6", "").Replace("7", "").Replace("8", "").Replace("9", ""); var groups = s1.ToCharArray().GroupBy(c => c); int bc = 0; foreach (var char_group in groups) bc++; Console.WriteLine("Количество слов: " + count); Console.WriteLine("Первое слово: " + s1a[0]); Console.WriteLine("Количество разных букв: " + bc); Console.ReadLine(); } } } Код using System; using System.Linq; namespace test2 { class Program { static void Main() { string s1 = "Michael have 20 fingers."; int count = 0; string[] s1a = s1.Split(' '); foreach (string q in s1a) { if (!int.TryParse(q, out int qq)) count++; } s1 = s1.Replace(" ", "").Replace(".", "").Replace("0", "").Replace("1", "").Replace("2", "").Replace("3", "") .Replace("4", "").Replace("5", "").Replace("6", "").Replace("7", "").Replace("8", "").Replace("9", ""); var groups = s1.ToCharArray().GroupBy(c => c); int bc = 0; foreach (var char_group in groups) bc++; Console.WriteLine("Количество слов: " + count); Console.WriteLine("Первое слово: " + s1a[0]); Console.WriteLine("Количество разных букв: " + bc); Console.ReadLine(); } } }
xmka, ты про регулярные выражения слышал? я, конечно, не C# кодер (я по C++), но этот код читать больно...
MrMystery, не поверишь слышал, поэтому и написал, что это самый простой код, тот который точно подойдёт для начала.
Можно черези Linq и Regex. Код склепаный на скорую руку, но думаю идея понятна, как это решить в пару строк.) using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace ConsoleApp2 { class Program { static void Main() { string str = "Michael have 20 fingers."; string[] words = (from s in str.Split(' ') where !Int32.TryParse(s, out int res) select s).ToArray(); Regex regex = new Regex("[a-z]"); HashSet<char> unic = new HashSet<char>(str.ToLower().ToCharArray().Where(x=> regex.IsMatch(x.ToString())).ToArray()); Console.WriteLine($"Количество слов: {words.Length}\n" + $"Первое слово: {words[0]}\n" + $"Количество разных букв: {unic.Count}"); Console.ReadLine(); } } } Код using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace ConsoleApp2 { class Program { static void Main() { string str = "Michael have 20 fingers."; string[] words = (from s in str.Split(' ') where !Int32.TryParse(s, out int res) select s).ToArray(); Regex regex = new Regex("[a-z]"); HashSet<char> unic = new HashSet<char>(str.ToLower().ToCharArray().Where(x=> regex.IsMatch(x.ToString())).ToArray()); Console.WriteLine($"Количество слов: {words.Length}\n" + $"Первое слово: {words[0]}\n" + $"Количество разных букв: {unic.Count}"); Console.ReadLine(); } } }