logo CodeStepByStep logo

mostCommon

Language/Type: C++ basics streams file input
Related Links:
Author: Marty Stepp (on 2016/06/16)

Write a function named mostCommon that accepts a string parameter representing a file name, then opens/reads that file's contents, and returns the word that occurs most frequently in the file. Words are separated by one or more spaces. Assume that any duplicate words will occur consecutively. If there is a tie, use the earlier word. You may assume that the string contains at least one word. For example, the following file's data will cause your function to return "Kim" because the word Kim occurs 3 times:

Ben Eric  Eric Marty Kim
Kim Kim  Jenny   Nancy
Nancy  Nancy  Paul   Paul
Type your C++ solution code here:


This problem asks for bare code. Submit a fragment of C++ code as described. Do not write any main function or any function heading around your code; just write the lines of code that will produce the result described.

You must log in before you can solve this problem.


Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.