Although string objects have some useful built in functions, there will often be times you want to do some work that they do not handle. In these cases, you may want to write your own function to build, parse, or modify a string.
For the most part, strings work with functions in the same ways as other data types. We can use strings as parameters and/or return types. Here is a function that takes two strings like "Ada" and "Lovelace" and returns a new string that combines them with a space:
Sometimes, doing a job with a string like “check if there is a vowel” or “make this all uppercase” actually means working character by character to do the work. Here is an example which makes an upper case version of a string by calling toupper with each character in the original string and using those to build up a new string:
Our current syntax for string parameters, (string s), is inefficient. (string s) makes a copy of whatever string is passed and stores that value in the function’s parameter. If you pass a very large string to a function, making that copy can be expensive.
Instead, we could use the syntax (const string& s). That tells the compiler to skip the copy and work with the original string (&) while making sure it does not get altered by accident (const).
We will learn about using references (&) later. For now, we will just live with making copies every time we pass a string. But if you like, you can skip ahead to learn about references (see Section 10.1and start using them now.
Build the function countLetter. It accepts a string and the letter to count as arguments. It should return the number of times the letter appears in the string.