Finding Number of words, Avg word length, and longest word in a sentence then ending it with a punctuation.

I need help with this question I don't know how to put it together. Finding the word count was as far as I could get.

Write a program to input a sentence, one word at a time, terminated by one of these three characters: . ! ? Then, output the number of words in the sentence, the average word length, and the longest word in the sentence (for ties, display the last one). For simplicity, assume that the only punctuation marks within the sentence are one of the two characters , ; where each punctuation mark is separated from the preceding word by a space.
Note: The user may enter the sentence over more than one input line.
Here is an example of what output should look like from running your program (user input is shown in bold):
Enter a sentence:
Alas , there was no more fuel ; we began
losing altitude rapidly !
Number of words = 11
Average word length = 4.54545
Longest word: altitude
Hints:
• Use a string class variable to read in each word and each punctuation mark. Since each word and each punctuation mark is separated by a space, use cin to read in the words and punctuation marks.
• Use the string variable’s length() function to determine the length of a word.
• Note: You must include #include <string> and include a using namespace std; statement in your program.
• Use relational operators to determine when punctuation has been read.


#include <iostream>
#include <string>
using namespace std;


int main(){

char str[200];
int w = 0, i =0;

cout << "Enter your sentence:" << " " << flush;
gets(str);

for (i = 0; str[i] != '\0'; i++){
if(str[i] != ' ' && str[i] != '\t' ){
w++;
while(str[i] != ' ' && str[i] != 't' )
i++;
}
}
cout << "Number of words =" <<" " << w;




return 0;
}

Last edited on
Hints: • Use a string class variable
Why don't you follow the instructions ?

gets(str);
Write a program to input a sentence, one word at a time
Again, why don't you follow the instructions ? You are not supposed to read the whole line.
I don't fully understand how to go about using functions in this situation.
I don't fully understand how to go about using functions in this situation.
No functions are necessary.

Use the professor's hints. He/she is telling you the easiest way to write the program.

Here is a program that uses the hints to read each word and punctuation of the input file and print it out on a line by itself. Modify this code to do what the assignment requires. Here are some more hints.

- Start by adding code to skip over punctuation marks within a sentence. Note that the assignment specifically says that you only need to worry about , and ;

- Next, add code to stop the loop at the end of a sentence (defined as one of these three characters: ? . ! ).

- Now you have a program that reads a "sentence" and parses out the words. Add code to count the number of words and print out the answer.
- Then add code to compute the average word length and print the answer.
- Then add code to determine the longest word and print the answer.
- You're done!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::string;

int main()
{
    string word;
    while (cin >> word) {	// read white-space-separated words
	cout << word << '\n';	// print the word on it's own line
    }
}

Ok thank you i'll give it another try.
Topic archived. No new replies allowed.