C + + Primer (fourth edition) after class exercise 6.8

Source: Internet
Author: User
Tags case statement

Problem:

Modify the vowel statistics program to make it count the number of spaces, tabs, and line breaks that are read in

Code:

#include <iostream>using namespace Std;int main () {char ch;string str;int acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, UCn  t = 0, spacecnt = 0;int brcnt = 0, tabcnt = 0;while (cin.get (CH)) {switch (ch) {case ' a ': Case ' a ': Acnt++;break;case ' e ': case ' E ': ecnt++;break;case ' i ': Case ' I ': Icnt++;break;case ' o ': Case ' o ': Ocnt++;break;case ' u ': Case ' u ': Ucnt++;break;case ' : Spacecnt++;break;case ' \ n ': brcnt++;break;case ' \ t ': tabcnt++;break;default:break;}} cout<<acnt<< "<<eCnt<<" "<<iCnt<<" "<<oCnt<<" <<uCnt< <endl;cout<<brCnt<< "<<tabCnt<<" "<<spacecnt<<endl;return 0;}

Summarize:

(1) The >> input operator of CIN treats null characters (spaces, tabs, and carriage returns) as the end of the input, so special characters such as spaces, carriage returns, and tab are obtained using the Cin.get (ch) method.

(2) The case label in the switch statement must be an integer constant expression, and each case statement uses a break to jump out of the loop.

(3) Strstr () searches for the first occurrence of a string in another string. When the searched string is found, the function returns the address of the first matching string, or null if the searched string is not found.

C + + Primer (fourth edition) after class exercise 6.8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.