TheBulgarianWolf

CountCharsInAString

Feb 22nd, 2021
1,651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CountCharsInAString
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Console.WriteLine("Enter your string here: ");
  11.             string line = Console.ReadLine();
  12.             Dictionary<char, int> charList = new Dictionary<char, int>();
  13.             foreach(char c in line)
  14.             {
  15.                 if((int)c == 32)
  16.                 {
  17.                     continue;
  18.                 }
  19.                 else if (charList.ContainsKey(c))
  20.                 {
  21.                     charList[c]++;
  22.                 }
  23.                 else
  24.                 {
  25.                     charList.Add(c, 1);
  26.                 }
  27.             }
  28.  
  29.             foreach(var ch in charList)
  30.             {
  31.                 Console.WriteLine(ch.Key + " -> " + ch.Value);
  32.             }
  33.         }
  34.     }
  35. }
  36.  
Add Comment
Please, Sign In to add comment