Question: Traversing a string (or any other type of array for that matter) is a common programming task. This assignment is the first part in a

Traversing a string (or any other type of array for that matter) is a common programming task. This assignment is the first part in a two-part series (the other is Assignment 3.5) where we will learn different techniques for visiting every member of a string. Your assignment is to write the function countLetters() then a driver program to test it.

countLetters()

Write a function to return the number of letters in a string. This involves traversing the string using the array notation (with an index as we have been doing all semester). We will re-write this function in Assignment 3.5 to do the same thing using a pointer.

main()

Create a main() that prompts the user for a line of input (using getline), calls countLetters(), and displays the number of letters.

Note that since the first cin will leave the stream pointer on the newline character, you will need to use cin.ignore() before getline() to properly fetch the section input. Take the first example below, the input buffer will look like:

z N o Z ' s H e r e !

If the program inputs a character followed by a line, the code will look like this:

cin >> letter; cin.getline(text, 256); 

After the first cin, the input pointer will point to the 'z'. When the getline statement gets executed next, it will accept all input up to the next newline character. Since the pointer is already on the newline character, the result will be an empty string. To skip this newline character, we use cin.ignore().

Example

Two examples The user input is in cursive

Example 1:

Enter a letter: z Enter text: NoZ'sHere! Number of 'z's: 0 

Example 2:

Enter a letter: a Enter text: Flowers are great Number of 'a's: 2 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!