fork download
  1. #include <stdio.h>
  2.  
  3. void listAlpha( char ch)
  4. {
  5. printf(" %c", ch);
  6.  
  7. }
  8.  
  9. char findCommonest(const char* arr){
  10. char commonest='@'; //Arbitrary Bad value!
  11. int high_count=0;
  12. for(int ch=0;ch<10;++ch){
  13. char counting=arr[ch];
  14. int count=0;
  15. for(int c=0;c<10;++c){
  16. if(arr[c]==counting){
  17. ++count;
  18. }
  19. }
  20. if(count>high_count){
  21. high_count=count;
  22. commonest=counting;
  23. }
  24. }
  25. return commonest;
  26. }
  27.  
  28.  
  29. int readAlpha(){
  30. char arr[10];
  31. int count = 1, iterator = 0;
  32. for(int iterator=0; iterator<10; iterator++){
  33. //printf("\nAlphabet %d:", count);
  34. scanf(" %c", &arr[iterator]);
  35. printf("\nAlphabet %d: %c", count,arr[iterator]);
  36. count++;
  37. }
  38. printf("\n-----------------------------------------\n");
  39. printf("List of alphabets: ");
  40. for (int x=0; x<10; x++)
  41. {
  42.  
  43. /* I’m passing each element one by one using subscript*/
  44. listAlpha(arr[x]);
  45. }
  46. //printf("%c",arr);
  47. printf("\nAlphabet with highest hit is %c\n",findCommonest(arr));
  48. return 0;
  49. }
  50.  
  51. int testFindCommonest(const char* arr,char expect){
  52. const char result=findCommonest(arr);
  53. if(result!=expect){
  54. printf("* Failed: Expected %c, Result %c\n",expect,result);
  55. return 1;
  56. }
  57. return 0;
  58. }
  59.  
  60. int unitTest(){
  61. int errors=0;
  62. const char test1[10]={'R','A','N','D','O','M','E','F','G','H'};
  63. errors+=testFindCommonest(test1,'R');
  64.  
  65. const char test2[10]={'A','A','A','A','A','A','A','A','A','A'};
  66. errors+=testFindCommonest(test2,'A');
  67. const char test3[10]={'A','B','B','C','C','C','E','Z','D','X'};
  68. errors+=testFindCommonest(test3,'C');
  69. const char test4[10]={'A','B','B','C','C','C','E','B','D','X'};
  70. errors+=testFindCommonest(test4,'B');
  71.  
  72. if(errors!=0){
  73. printf("** FAILED **\n");
  74. }else{
  75. printf("* SUCCESS *\n");
  76. }
  77. return errors;
  78. }
  79.  
  80. int main(){
  81. unitTest();
  82. //readAlpha();
  83. return 0;
  84. }
Success #stdin #stdout 0s 4376KB
stdin
ABCABABCBE
stdout
* SUCCESS *