fork download
  1. // OrinTill Qimid CS1A Chapter 4, P. 294, 10
  2. //
  3. // /**************************************************************
  4. // * MOST FREQUENT CHARACTER FINDER
  5. // *
  6. // * ____________________________________________________________
  7. // * This program uses two overloaded functions to handle both
  8. // * C-string and std::string inputs. The main function demonstrates
  9. // * the functionality by asking the user to enter a string, calling
  10. // * the appropriate function, and displaying the result.
  11. // * ____________________________________________________________
  12. // * INPUT
  13. // * A string provided by the user.
  14. // * OUTPUT
  15. // * The most frequent character in the input string.
  16. // **************************************************************/
  17.  
  18. #include <iostream>
  19. #include <cstring> // For C-string functions
  20. #include <string> // For std::string
  21.  
  22. using namespace std;
  23.  
  24. // Function to find the most frequent character in a C-string
  25. char mostFrequentChar(const char* str) {
  26. int freq[256] = {0}; // Array to store frequency of each ASCII character
  27.  
  28. // Count character frequencies
  29. for (int i = 0; str[i] != '\0'; i++) {
  30. if (!isspace(str[i])) { // Ignore spaces
  31. freq[static_cast<unsigned char>(str[i])]++;
  32. }
  33. }
  34.  
  35. // Find the character with the highest frequency
  36. char mostFrequent = '\0';
  37. int maxCount = 0;
  38.  
  39. for (int i = 0; i < 256; i++) {
  40. if (freq[i] > maxCount) {
  41. maxCount = freq[i];
  42. mostFrequent = static_cast<char>(i);
  43. }
  44. }
  45.  
  46. return mostFrequent;
  47. }
  48.  
  49. // Overloaded function to find the most frequent character in a std::string
  50. char mostFrequentChar(const string& str) {
  51. int freq[256] = {0}; // Array to store frequency of each ASCII character
  52.  
  53. // Count character frequencies
  54. for (char ch : str) {
  55. if (!isspace(ch)) { // Ignore spaces
  56. freq[static_cast<unsigned char>(ch)]++;
  57. }
  58. }
  59.  
  60. // Find the character with the highest frequency
  61. char mostFrequent = '\0';
  62. int maxCount = 0;
  63.  
  64. for (int i = 0; i < 256; i++) {
  65. if (freq[i] > maxCount) {
  66. maxCount = freq[i];
  67. mostFrequent = static_cast<char>(i);
  68. }
  69. }
  70.  
  71. return mostFrequent;
  72. }
  73.  
  74. int main() {
  75. const int SIZE = 256; // Maximum size for C-string
  76. char cString[SIZE];
  77. string stdString;
  78.  
  79. // Get input as a C-string
  80. cout << "Enter a string (C-string): ";
  81. cin.getline(cString, SIZE);
  82.  
  83. // Call the function for C-string
  84. char cStringResult = mostFrequentChar(cString);
  85. cout << "The most frequent character in the C-string: " << cStringResult << endl;
  86.  
  87. // Get input as a std::string
  88. cout << "Enter a string (std::string): ";
  89. getline(cin, stdString);
  90.  
  91. // Call the overloaded function for std::string
  92. char stdStringResult = mostFrequentChar(stdString);
  93. cout << "The most frequent character in the std::string: " << stdStringResult << endl;
  94.  
  95. return 0;
  96. }
Success #stdin #stdout 0s 5264KB
stdin
Standard input is empty
stdout
Standard output is empty