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

Source: Internet
Author: User

Problem:

Reads a piece of text into a vector object, and each word is stored as an element in the vector. Put the Vector object

Each word is converted to uppercase. Outputs the converted elements in the vector object, with each 8 words being one line of output.

Code:

1#include <iostream>2#include <vector>3 4 using namespacestd;5 6 intMain ()7 {8cout<<"Enter a text (Ctrl + Z end):"<<Endl;9vector<string>Vecstr;Ten     stringWord; One      while(cin>>word) A     { - Vecstr.push_back (word); -     } the     if(vecstr.size () = =0) -     { -cout<<"No input string"<<Endl; -         return-1; +     } -      for(vector<string>::size_type i =0; I < vecstr.size (); i++) +     { A          for(string:: Size_type j =0; J < Vecstr[i].size (); J + +) at         { -VECSTR[I][J] =ToUpper (Vecstr[i][j]); -         } -cout<<vecstr[i]<<" ";  -         if((i+1) %8==0) -         { incout<<Endl; -         } to     } +     return 0; -}
View Code

Summarize:

(1) Vector<type>,string method size Returns a value of type Vector<type>::size_type and String::size_type;

(2) Read input data multiple times using while (CIN>>STR) {};

(3) to determine whether the vector is empty, good programming habits;

(4) The method of converting the case with string, ToUpper (c), ToLower (c);

(5) Use ((i + 1)% 8) = = 0 To judge the line, form concise.

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

Related Article

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.