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)