Question

Write two versions of the function mostFrequentCharacter, one that accepts a C-string and one that accepts...

Write two versions of the function mostFrequentCharacter, one that accepts a C-string and one that accepts a string object, as its argument. The function should return the character that appears most frequently in the string. Demonstrate the function in a complete program.  Do not use any string or cstring functions in the cstring version.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:

#include <iostream>
#include <cstring>
#define ASCII_SIZE 256
using namespace std;

char getMaxOccuringChar(char* str)
{
   // Create array to keep the count of individual
   // characters and initialize the array as 0
   int count[ASCII_SIZE] = {0};

   // Construct character count array from the input
   // string.
   int len = strlen(str);
   int max = 0; // Initialize max count
   char result; // Initialize result

   // Traversing through the string and maintaining
   // the count of each character
   for (int i = 0; i < len; i++) {
       count[str[i]]++;
       if (max < count[str[i]]) {
           max = count[str[i]];
           result = str[i];
       }
   }

   return result;
}

char getMaxOccuringChar(string str)
{
   // Create array to keep the count of individual
   // characters and initialize the array as 0
   int count[ASCII_SIZE] = {0};

   // Construct character count array from the input
   // string.
   int len = str.length();
   int max = 0; // Initialize max count
   char result; // Initialize result

   // Traversing through the string and maintaining
   // the count of each character
   for (int i = 0; i < len; i++) {
       count[str[i]]++;
       if (max < count[str[i]]) {
           max = count[str[i]];
           result = str[i];
       }
   }

   return result;
}

int main(){

   char str[100];
   cout<<"Enter String : ";
   cin>>str;
   char c=getMaxOccuringChar(str);
   cout<<"Most frequent character is : "<<c<<endl;

   string str2;
   cout<<"Enter String : ";
   cin>>str2;
   c=getMaxOccuringChar(str2);
   cout<<"Most frequent character is : "<<c<<endl;

   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write two versions of the function mostFrequentCharacter, one that accepts a C-string and one that accepts...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • To be written in C++ write a recursive function that accepts a string object as its...

    To be written in C++ write a recursive function that accepts a string object as its argument and prints the string in reverse order. demonstrate the function in a driver program.

  • c++ Write a function that uses recursion to raise a number to a power. The function...

    c++ Write a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program. Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will...

  • write a C++ function that accepts a pointer to a C-string as an argument and displays...

    write a C++ function that accepts a pointer to a C-string as an argument and displays its contents backward. For instance, if the string argument is “ Gravity ” the function should display “ ytivarG ”. Demonstrate the function in a program that asks the user to input a string and then passes it to the function.

  • using c++ program write this program without the optional exercise 3. Word Counter Write a function...

    using c++ program write this program without the optional exercise 3. Word Counter Write a function that accepts a pointer to a C-string as an argument and returns the number of words contained in the string. For instance, if the string argument is "Four score and seven years ago” the function should return the number 6. Demonstrate the function in a program that asks the user to input a string and then passes it to the func tion. The number...

  • Write a function that accepts a pointer to a C-string as its argument. The function should...

    Write a function that accepts a pointer to a C-string as its argument. The function should count the number of times the character 'w’ occurs in the argument and return that number.

  • Write a program with a function that accepts a string as an argument and returns a...

    Write a program with a function that accepts a string as an argument and returns a copy of the string with the first character of each sentence capitalized. For instance, if the argument is “hello. my name is Joe. what is your name?” the function should return the string “Hello. My name is Joe. What is your name?” The program should let the user enter a string and then pass it to the function. The modified string should be displayed.

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • 1. String Length Write a function that returns an integer and accepts a pointer to a...

    1. String Length Write a function that returns an integer and accepts a pointer to a C-string as an argument. The function should count the number of characters in the string and return that number. Demonstrate the function in a simple program that asks the user to input a string, passes it to the function, and then displays the function’s return value. c++

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT