logo CodeStepByStep logo

mostCommonFirstChar

Language/Type: C++ Collections map set STL
Related Links:

Write a function named mostCommonFirstChar that accepts a reference to a set of strings as a parameter, and returns a character (a char value) indicating the character that appears at the start of the most strings in the set, in lowercase. For example, if a set called words containing the following elements:

{"Julie", "hi", "how", "is", "He", "Marty!", "this", "morning?", "fine.", "?huh?", "HOW", "I"}

One word in the list begins with 'f', four begin with 'h', two begin with 'i', one begins with 'j', two begin with 'm', one begins with 't', and one begins with '?'. Therefore the call of mostCommonFirstChar(words) should return 'h'.

If there is a tie, return the character that comes first in alphabetical order (that has the lowest ASCII value). If any of the set's elements is an empty string, it does not have a first letter, so it should be excluded from your computation.

You may assume that the set passed is non-empty. You are allowed to create one auxiliary collection to help you solve this problem. Do not modify the contents of the set passed to your function.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.