Question 1510: Replace space-nine degrees

Source: Internet
Author: User

Description:
Implement a function to replace spaces in a string with "% 20 ". For example, if the string is we are happy, the replaced string is we % 20are % 20 happy.
Input:
Each input file contains only one set of test samples.
For each group of test cases, enter a line to indicate the strings to be processed.
Output:
Corresponding to each test case, the processed string is output.
Sample input:
We are happy
Sample output:

We % 20are % 20 happy

Recommendation index :※

Source: http://ac.jobdu.com/problem.php? PID = 1, 1510

This is also the title of Tao's "Sword refers to offer.

#include<iostream>#include<string>#include<sstream>#include<string.h>#include<stdio.h>using namespace std;int main(){char ch;while((ch=getchar())!='\n'){if(ch!=' ')printf("%c",ch);elseprintf("%%20");}    return 0;}

Originally written a stringstream, but in Getline, two carriage return is not very good processing (solution: http://support.microsoft.com/default.aspx? SCID = KB; en-US; q240015 & id = KB; en-US; q240015), wood has AC (rewrite the Getline function ?).

#include<iostream>#include<string>#include<sstream>#include<string.h>#include<stdio.h>using namespace std;int main(){    string str,strout;    getline(cin,str);//need to enter    stringstream streamstr;streamstr<<str;    int flag=0;    while(streamstr>>strout){        if(flag!=0)            cout<<"%20"<<strout;        else{            cout<<strout;flag=1;}    }    return 0;}

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.