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