Nyoj 113 String Substitution (C++stl solution)

Source: Internet
Author: User

string SubstitutionTime limit: theMs | Memory Limit:65535KB Difficulty:2
Describe
Write a program implementation to replace All "you" in the string with "we"
Input
Input contains multiple rows of data

Each row of data is a string with a length of not more than 1000
Data ends with EOF
Output
For each line entered, the output of the replaced string
Sample input
Do
Sample output
We are and we do

Also has done many times the water question, recently learned the STL, uses this question to be familiar with the find (), replace () function.

The specific replace () function is shown in the previous post: Click the Open link.

The Find () function is a lookup function in string that is similar to replace () and replace () is a substitution function in string.

This blog post does not give a detailed description of the Find () function, the specific use can be Baidu.

The topic is simple, the code is as follows:

#include <iostream> #include <algorithm> #include <string.h>using namespace Std;int main () {string S, S1,s2;s1= "You"; s2= "we", int sign;while (Getline (cin,s)) {sign=s.find (s1,0); while (Sign!=string::npos) {S.replace ( SIGN,3,S2); Sign=s.find (s1,sign+1);} Cout<<s<<endl;} return 0;}


Note:

String::npos 's understanding: The String class provides 6 lookup functions, each of which is named after different forms of find.
All of these operations return a value of type String::size_type, which marks the position where the lookup match occurred;
or return a special value named String::npos, which indicates that the lookup does not match. The string class defines NPOs as guaranteed to be greater than any valid underlying value.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nyoj 113 String Substitution (C++stl solution)

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.