SHARE
TWEET

CountLettersInText

JavaFan Jan 21st, 2014 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _21.PrintLettersCountInString
  8. {
  9.     class Program
  10.     {
  11.         // 21. Write a program that reads a string from the console and prints all different letters in the string
  12.         //     along with information how many times each letter is found.
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             Console.WriteLine("Insert text:");
  17.             string text = Console.ReadLine();
  18.  
  19.             StringBuilder builder = new StringBuilder();
  20.             StringBuilder count = new StringBuilder();
  21.  
  22.             for (int i = 0; i < text.Length; i++)
  23.             {
  24.                 bool check = false;
  25.                 int position = 0;
  26.                 for (int j = 0; j < builder.Length; j++)
  27.                 {
  28.                     if (builder[j] == text[i])
  29.                     {
  30.                         check = true;
  31.                         position = j;
  32.                         break;
  33.                     }
  34.                 }
  35.  
  36.                 if (check == false)
  37.                 {
  38.                     builder.Append(text[i]);
  39.                     count.Append(1);
  40.                 }
  41.                 else
  42.                 {
  43.                     count[position]++;
  44.                 }
  45.             }
  46.             //Console.WriteLine(string.Join(" ", builder.ToString()));
  47.             //Console.WriteLine(string.Join(" ", count.ToString()));
  48.  
  49.             for (int i = 0; i < builder.Length; i++)
  50.             {
  51.                 Console.WriteLine("Char: {0} Times: {1}" , builder[i], count[i]);
  52.             }
  53.         }
  54.     }
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top