Advertisement
JavaFan

CountLettersInText

Jan 21st, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement