C + + self-study note _string Test programming Question _ C + + Primer

Source: Internet
Author: User
Tags uppercase letter

Today, "C + + Primer" finish the 9th chapter, about string type, find a way to test the effect of the topic.
Title Description:

Enter a string to end with a carriage return (string length <=100). The string consists of several words, separated by a space, and all the words are case-sensitive. Now you need to replace one of these words with another word and output the replaced string.

Input:
Multiple sets of data. Each set of data input consists of 3 rows, the 1th line is a string with multiple words, the 2nd line is the word a to be replaced, and (length <=100) line 3rd is the word b that will be replaced. (length <=100) s, a, b the front and the back have no spaces.

Output:
Each test data output has only 1 rows, replacing all word a in s with the string after B.

Sample input:
Want someone to help Youyoui
Sample output:
I want someone to help you


With the STL, good cool!!
Code:
#include <iostream>#include<string>#include<cstdio>using namespacestd;intMain () {stringStr,word; stringword_a,word_b;  while(Getline (cin,str)) {getline (cin,word_a);        Getline (cin,word_b); intstartpos=0, endpos=0;  while((Startpos=str.find_first_not_of (" ", endpos))! =string:: NPOs) {Endpos=str.find_first_of (" ", startpos); Word.assign (Str,startpos,endpos-startpos); if(word==word_a)            {str.replace (Startpos,word_a.size (), word_b); }} cout<<str<<Endl; }    return 0;} /************************************************************** problem:1111 User:lcyvino language:c++ Re sult:accepted time:0 Ms memory:1520 kb****************************************************************/

Title Description:

For all words in a string, if the first letter of the word is not an uppercase letter, the first letter of the word becomes uppercase.
In the string, the words are separated by a white space character, including a space ('), a tab (' \ t '), a carriage return (' \ R '), and a newline character (' \ n ').

Input:

Enter a line: the string to be processed (less than 100 in length).

Output:

There may be multiple sets of test data, for each set of data,
Output one line: the converted string.

Sample input:
If so, you already has a Google account. You can sign in to the right.
Sample output:
If So, you already has A Google account. You Can sign in to the right.
Source:
2008 the computer research of the graphics Laboratory of Peking University the real Problem


There is nothing to say, or STL.
Code:
#include <iostream>#include<string>using namespacestd;intMain () {stringstr; stringSeperators (",. \t\r\n");  while(Getline (cin,str)) {intstartpos=0, endpos=0;  while((Startpos=str.find_first_not_of (seperators,endpos))! =string:: NPOs) {Endpos=str.find_first_of (Seperators,startpos); if(Islower (Str[startpos])) {Str[startpos]=ToUpper (Str[startpos]); }} cout<<str<<Endl; }    return 0;} /************************************************************** problem:1121 User:lcyvino language:c++ Re sult:accepted time:10 Ms memory:1520 kb****************************************************************/

Title Description:

Compile a program, read the user input, and "." The end of a line of text, counting the total number of words, and separately output each word contains how many characters.
(the part that is separated by one or more spaces is a single word)

Input:

The input includes 1 lines of string to "." Ends with multiple words in the string, separated by one or more spaces between the words.

Output:

There may be multiple sets of test data, for each set of data,
The number of letters that each word in the output string contains.

Sample input:
Hello how is you.
Sample output:
5 3 3 3


Code:
#include <iostream>#include<string>using namespacestd;intMain () {stringstr; stringSeperators (" ."); intwordlength;  while(Getline (cin,str)) {BOOLisfirst=true; intstartpos=0, endpos=0;  while((Startpos=str.find_first_not_of (seperators,endpos))! =string:: NPOs) {Endpos=str.find_first_of (Seperators,startpos); Wordlength=endpos-startpos; if(isFirst) {cout<<wordlength; IsFirst=false; }Else{cout<<" "<<wordlength; }} cout<<Endl; }    return 0;} /************************************************************** problem:1182 User:lcyvino language:c++ Re sult:accepted time:20 Ms memory:1520 kb****************************************************************/

C + + self-study note _string Test programming Question _ C + + Primer

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.