Replace spaces in strings

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%20Happy

--------------------------------------------------------------------------

Let's start with a funny version of the original AC code:

#include<stdio.h>#include<stdlib.h> int main(){    char str[1000000];    gets(str);    char *p = str;    while(*p != '\0')    {        if(*p == ' ')        {            printf("%s","%20");                 }else        {            printf("%c",*p);                 }        p++;    }    printf("\n");    return 0;}/**************************************************************    Problem: 1510    User: Rowandjj    Language: C++    Result: Accepted    Time:30 ms    Memory:1928 kb****************************************************************/

Of course, this is not the intention of this question, haha ..

The idea should be as follows:

1. First, traverse the string and calculate the string length Len and the number of spaces in the string num. The length of the new string should be len + num * 2. 2. Create two pointers pointing to the original string and the replaced string tail position respectively. Copy From back to forwardIn case of space, replace it with % 20. Code:

/******************************** Replace space by rowandjj2014/7/16 ********************************/# include <iostream> # include <stdio. h> using namespace STD; # define Max 1000000 // Replace space (based on the original string) void replaceblank (char string [], int Len) // Len is the total length of the character array {If (string = NULL | Len <= 0) {return;} int I = 0; int o_len = 0; // valid character length of the original string int B _len = 0; // number of spaces while (string [I]! = '\ 0') {o_len ++; If (string [I] = '') {B _len ++;} I ++; // do not forget I auto-increment, otherwise, always returns} int n_len = o_len + B _len * 2; // The length of the replaced string if (n_len> Len) // The length of the new string is greater than the total length of the character array {return;} int n_index = n_len; // pointer to the new string, starting with int o_index = o_len; // The pointer to the original string while (o_index> = 0 & n_index> o_index) // replace it with a forward copy from the back, the advantage is to reduce the number of characters to be moved {If (string [o_index] = '') // when pointing to a space {string [n_index --] = '0 '; string [n_index --] = '2'; string [n_index --] = '%';} else {string [n_index --] = string [o_index];} o_index --;}} int main () {char STR [Max]; char * P = STR; gets (p); replaceblank (STR, max); cout <p <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.