http://ac.jobdu.com/problem.php?pid=1510
Topic Description:
Implement a function that replaces a space in a string with "%20." For example, when the string is Are Happy, the replaced string is We%20are%20happy.
Input:
Each input file contains only one set of test samples.
For each set of test cases, enter a line representing the string to be processed.
Output:
corresponding to each test case, the processed string is given.
Sample input:
We Are Happy
Sample output:
We%20are%20happy
Don't know why the wrong code:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main () {
string s;
Vector<string> Svec;
while (Getline (CIN, s)) {
StringStream ss;
string S1;
SS << S;
while (SS >> S1)
Svec.push_back (S1);
int ssize = Svec.size ();
for (int i = 0; i < ssize ++i) {
cout << svec[i] << ((i+1-ssize)? " %20 ": \ n");
}
return 0;
}
Simple AC Code:
#include <cstdio>
int main () {
char ch;
while (ch = getchar ())!= ' \ n ') {
if (ch!= ')
printf ("%c", ch);
else
printf ("%%20");
}
printf ("\ n");
return 0;
}
Another solution:
#include <cstdio>
#include <cstring>
char str[1000000];
int main () {
gets (str);
int len = strlen (str);
int count = 0;
for (int i = 0; i < len; ++i) {
if (str[i] = = ")
++count;
}
int newlen = len + count*2;
Str[newlen] = 0;
while (len >= 0) {
if (Str[len]!= ') {
Str[newlen] = Str[len];
--len;
--newlen;
}
else{
Str[newlen] = ' 0 ';
Str[newlen-1] = ' 2 ';
Str[newlen-2] = '% ';
Newlen-= 3;
--len
}
}
Puts (str);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main () {
int count;
int size;
string S;
while (Getline (CIN, s)) {
count = 0;
Size = S.size ();
for (int i = 0; i < size; ++i)
if (s[i] = = ")
++count;
S.resize (size + 2 * count);
for (int i = size; I >= 0;-i) {
if (s[i] = = ') {
s[i + 2 * count] = ' 0 '
; S[i + 2 * count-1] = ' 2 ';
S[i + 2 * count-2] = '% ';
--count;
} else {
S[i + 2 * count] = S[i];
}
cout << s << endl;
}
return 0;
}