Advertisement
vovanhik_24

Task 2

Apr 8th, 2025
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Task2
  5. {
  6.     public class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Console.Write("Введите строку: ");
  11.             string input = Console.ReadLine() ?? string.Empty; // защита от null
  12.  
  13.             int uniqueCount = CountUniqueChars(input);
  14.  
  15.             Console.WriteLine($"Количество уникальных символов в строке \"{input}\": {uniqueCount}");
  16.         }
  17.  
  18.         public static int CountUniqueChars(string word)
  19.         {
  20.             if (string.IsNullOrEmpty(word))
  21.                 return 0;
  22.  
  23.             HashSet<char> uniqueChars = new HashSet<char>();
  24.  
  25.             foreach (char ch in word)
  26.             {
  27.                 uniqueChars.Add(ch);
  28.             }
  29.  
  30.             return uniqueChars.Count;
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement