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;}