31-language Primer -31-string substitution

Source: Internet
Author: User

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

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.