Write a C# program to find maximum occurring character in a string


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. public class Exercise10
  3. {
  4. public static void Main()
  5. {
  6. string str;
  7. int[] frequency = new int[255];
  8. int i = 0, max, l;
  9. int ascii;
  10.  
  11. Console.Write("Enter the string : ");
  12. str = Console.ReadLine();
  13. l = str.Length;
  14.  
  15. for (i = 0; i < 255; i++)
  16. {
  17. frequency[i] = 0;
  18. }
  19. // Reading frequency of each characters
  20. i = 0;
  21. while (i < l)
  22. {
  23. ascii = (int)str[i];
  24. frequency[ascii] += 1;
  25.  
  26. i++;
  27. }
  28.  
  29. max = 0;
  30. for (i = 0; i < 255; i++)
  31. {
  32. if (i != 32)
  33. {
  34. if (frequency[i] > frequency[max])
  35. max = i;
  36. }
  37. }
  38. Console.Write("The Highest frequency of character '{0}' is appearing for number of times : {1} \n\n", (char)max, frequency[max]);
  39.  
  40. Console.ReadLine();
  41. }
  42. }

Result

C# language techstudy

     





Related Topics




Popular Posts