Question

Write a program in C plus plus that reads input a word at a time until...

Write a program in C plus plus that reads input a word at a time until a lone q is entered.The

program should then report the number of words that began with vowels, the num-
ber that began with consonants,and the number that fit neither of those categories.

One approach is to use isalpha() to discriminate between words beginning with

letters and those that don’t and then use an if or switch statement to further iden-
tify those passing the isalpha() test that begin with vowels.A sample run might

look like this:
Enter words (q to quit):
The 12 awesome oxen ambled
quietly across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
   char pres, prev=' ', p_prev = ' ';
   int v_count = 0, c_count = 0, o_count = 0;
  
   cout << "\nEnter words(q to quit):" << endl;
  
   while( true )
   {
       pres = fgetc(stdin);
       if( prev == ' ' || prev == '\n' )
       {  
           if(isalpha(pres))
           {
                   if( pres=='a' || pres=='A' || pres=='e' || pres=='E' ||
                       pres=='i' || pres=='I' || pres=='o' || pres=='O' ||
                       pres=='u' || pres=='U')
                   {
                       v_count++;
                   }
                   else
                   {
                       c_count++;
                   }
           }
           else
           {
               o_count++;
           }
       }
       else
       {
           if((p_prev == ' '|| p_prev == '\n') && prev == 'q'
                 && (pres == ' '|| pres == '\n'))
           {
               c_count--;
                 break;          
           }
       }
       p_prev = prev;
       prev = pres;
   }
  
   cout<<v_count<<" words beginning with vowels\n";
   cout<<c_count<<" words beginning with consonants\n";
   cout<<o_count<<" others\n\n";
  
   return 1;
}

Execution and Output:

Add a comment
Know the answer?
Add Answer to:
Write a program in C plus plus that reads input a word at a time until...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program, called wordcount.c, that reads one word at a time from the standard input....

    Write a program, called wordcount.c, that reads one word at a time from the standard input. It keeps track of the words read and the number of occurrences of each word. When it encounters the end of input, it prints the most frequently occurring word and its count. The following screenshot shows the program in action: adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT