Topic 1510: Replace space

Source: Internet
Author: User
Tags int size

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


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.