Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 06. Count Chars in a String
- Write a program that counts all characters in a string except for space (' ').
- Print all the occurrences in the following format:
- {char} -> {occurrences}
- Examples
- Input Output
- text t -> 2
- e -> 1
- x -> 1
- text text text
- t -> 6
- e -> 3
- x -> 3
- using System;
- using System.Collections.Generic;
- namespace _01CountCharsInA_String
- {
- class Program
- {
- static void Main(string[] args)
- {
- var input = Console.ReadLine().Split();
- var countChars = new Dictionary<char, int>();
- for (int word = 0; word <= input.Length - 1; word++)
- {
- var text = input[word];
- for (int letter = 0; letter <= text.Length - 1; letter++)
- {
- if (countChars.ContainsKey(text[letter]))
- {
- countChars[text[letter]]++;
- }
- else
- {
- countChars.Add(text[letter], 1);
- }
- }
- }
- foreach (var item in countChars)
- {
- Console.WriteLine($"{item.Key} -> {item.Value}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement