2. Using the algorithm developed above, create a function that determines the status of the first character of a word. The function should receive a string containing a single word as the first parameter which should be defined as constant, and integer reference parameters containing the counts for the number of vowels, consonants and numbers found in a sentence. The main function should open a file containing sentences, read each sentence, and use the function to add to the counts for each word of the sentence. The main function should output each sentence read from the file and the total counts for each sentence. Hints: Each sentence from the file must be parsed into individual words which the function can examine. You may assume words will be separated by a single space.The find and substr string methods can assist in the parsing of the sentence. Alternatively, you may use a stringstream object to parse each line read from the file. See Strings.cpp and StringStream.cpp examples. Please see http://www.cplusplus.com/reference/string/string/e to learn more about using the find and substr functions and http://www.cplusplus.com/reference/sstream/istringstream/str/e to learn about the istringstream class. The text input can be created with any simple text editor such as NotePad. In jGrasp, text files can be created be selecting New | Plain Text from the File menu. Store the text file in the same folder as the program. Example run: Input string: There are 3 cats in the yard! 2 words begin with a vowel 4 words begin with a consonant 1 other 'words' were found Input string: Fall is a beautiful time of year. 3 words begin with a vowel 4 words begin with a consonant O other 'words' were found Input string: There are 12 inches in a foot. 4 words begin with a vowel 2 words begin with a consonant 1 other 'words' were found
Please use easy logic with proper indentations and comments for understanding!. Coding should be in C++. I am providing an
1
2
3 #include <iostream>
4 #include <fstream>
5 #include <cstdlib>
6
7 using namespace std;
8
9 void status(string st,int &vowel,int &consonant, int &other)
10 {
11
12 //if it is vowel
13 if ((st[0]=='a' || st[0] == 'e' || st[0] == 'i' || st[0] == 'o' || st[0]=='u') || (st[0]=='A' || st[0] == 'E' || st[0] == 'I' || st[0] == 'O' || st[0]=='U') )
14 vowel++; //incrementing vowels by 1
15 else if(isalpha(st[0]))
16 consonant++; //increment consonants by 1
17 else
18 other++; //increment others by 1
19 }
20 // main that will read the file
21 int main()
22 {
23 //word stores the word, filename stores the name of the file
24 string word;
25 string filename;
26 int vowels = 0,
27 consonants = 0,
28 others = 0;
29 ifstream file("file.txt");
30 // extracting words from the file using stream operator
31 cout<<"input string : ";
32 while (file >> word) { //while there are words to read
33 cout << word <<" "; //output word
34 status(word,vowels,consonants,others); //call function status and pass the word and other variables as parameters
35 }
36 //Printing the total vowel and consonants
37 cout<<"\n"<<vowels<<" words begin with a vowel "<<endl;
38 cout<<consonants<<" words begin with a consonant "<<endl;
39 cout<<others<<" other words were found "<<endl;
40 return 0;
41 }
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images