Sword refers to the offer series source code-replace space

Source: Internet
Author: User

Topic 1510: Replacement Space time limit: 1 seconds Memory limit: 128 Mega Special: No submission: 7383 Resolution: 1889 Title Description: Implement a function that replaces a space in a string with "%20". For example, when the string is we are Happy. The string after substitution is we%20are%20happy. Input: Each input file contains only one set of sample test specimens. For each set of test cases, the input line represents the string to be processed. Output: Corresponds to each test case, out of the processed string. Sample input: We are happy sample output: We%20are%20happy

#include <iostream> #include <stdio.h> #include <string.h>using namespace Std;int main () {    Char STR[1000000];    Gets (str);    int n = strlen (str);    int blanknum = 0;    for (int i=0;i<n;i++) {        if (' ==str[i]) {            blanknum++;        }    }    int indexnew = n+blanknum*2;//a space increment of 2 characters    //from the back to replace the for    (int i=n;i>=0;i--) {        if (str[i]== ') {            str[indexnew--] = ' 0 ';            str[indexnew--] = ' 2 ';            str[indexnew--] = '% ';        } else{            str[indexnew--]=str[i];        }    }    printf ("%s", str);    return 0;}

Sword refers to the offer series source code-replace space

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.