An answer for C++ excercise

It's shown on the C++ prime plus:

You would see it if you follow the step ..

I just want make a record, it takes me some times...

On page: 301


The excercise:

Write a program 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 number that began whith 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 identify 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...


I give a similiar sample, It the same function as the excercise.. of course , you can find another way to expression to get the result.. But it give us a order to use the isalpha()..

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

int main()
{
 char char_tmp[32];
 int words = 0, nums = 0, others = 0;
 char *ch;
 ch = char_tmp;
 while(true){
 cout << "Enter words ( q to quit): " << endl;
 while(*ch = cin.get()){
 if(ispunct(*ch)) others ++;
 if(ispunct(*ch)&& !isalpha(*(ch-1)))continue;
 if(isalpha(*ch) or isdigit(*ch)){ch++; continue;}
 if(*ch == 0xa && *(ch-1) == 'q'){ch=char_tmp;break;}
 if(*ch == ' ' && isalpha(*(ch-1))){words++; ch = char_tmp;}
 if(*ch == '\n' && isalpha(*(ch-1))){words++; ch = char_tmp;}
 if(*ch == ' ' && isdigit(*(ch-1))){nums++; ch = char_tmp;}
 if(*ch=='\n' && isdigit(*(ch-1))){nums++; ch = char_tmp;}
 if(isdigit(*ch)&&*(ch-1)=='.' && isdigit(*(ch-2))){nums++; ch = char_tmp;}
}
cout << "Words: " << words <<"\tNums: "<< nums << endl;
cout << "Punctuation_Symbols: " << others << endl;
words = 0;
others = 0;
nums = 0;
}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值