Title address: http://acm.nyist.net/JudgeOnline/problem.php?pid=113??
Description
Write a program implementation to put all the strings in the"You"replaced by"We"
input
input contains multiple rows of data
each row of data is a string with a length not exceeding1000
data toEOFEnd
Output
for each line entered, the output of the replaced string
Sample Input
Do
Sample OutputWe are and we do?? Code: #include <stdio.h>
Working with Data
static void Handlerdata (char *arr,int len);
int main ()
{
???? while (1)
???? {
????????? Char inputarr[1000] = {'} '};
????????? if (gets (inputarr) = = NULL)
?????????????? Break
????????? Handlerdata (inputarr,100);
????????? Puts (Inputarr);
???? }
????
???? return 0;
}
Working with Data
static void Handlerdata (char *arr,int len)
{
???? if (Len < 3)
????????? Return
?????????
???? You-we
???? int i = 2;
???? for (; i < len;++i)
???? {
????????? if (arr[i-2] = = ' + ' | | arr[i-1] = = '/' | | arr[i]== ' + ')
?????????????? Break
??????????????
????????? if (arr[i-2]== ' y ' && arr[i-1] = = ' O ' && arr[i] = = ' U ')
????????? {
?????????????? Arr[i-2] = ' W ';
?????????????? ARR[I-1] = ' e ';
?????????????? int flag = i;
??????????????
?????????????? while (Flag<len && arr[flag]! = ' \ "&& Flag+1<len)
?????????????? {
??????????????????? Arr[flag] = arr[flag+1];
??????????????????? ++flag;
?????????????? }
????????? }
???? }
The experience gained from previous questions is that the boundary is considered, and the code of the index in the subject is considered the boundary problem. The STL can be easily solved:
#include <algorithm>
#include <iostream>
#include <string>
using namespace Std;
int main ()
{
??? String s, s1, S2;
??? while (Getline (cin,s))
??? {
??????? int flag;
??????? S1 = "You";
??????? S2 = "we";
??????? Flag = S.find (s1,0);
??????? while (flag! = String::npos)
??????? {
??????????? S.replace (flag, 3, S2);
??????????? Flag = S.find (S1, flag + 1);
??????? }
??????? cout << s << endl;
??? }
??? return 0;
}
31-language Primer -31-string substitution