The sword refers to the offer analysis __ space substitution string problem

Source: Internet
Author: User

question: In the string "We are happy", replace all the spaces with the string%20. Analysis: If we do not define a new character array, replace it in the original character array, replacing one byte of space with a three-byte character array,%20, which produces an out-of-bounds access to the array, allowing us to open up a new space to hold the replaced string, The problem will become very simple. set two pointers to the first element of the old and new string, traverse the original string, and fill in the new string with "%20" if it touches a space, or copy the contents of the meta-string.
#include <stdio.h> #include <string.h> #include <assert.h>char *instead (const char *STRSRC, char * Strdes) {assert (STRSRC! = NULL && strdes! = null); char *p=strdes;char *pdes;const char *psrc;pdes = STRDES;PSRC = S Trsrc;while (*psrc! = ') {if (*psrc! = ") {*pdes++ = *PSRC;    Copy the original string content}else{memcpy (pDes, "%20", 3);    Encountered a space insert%20pdes + = 3 in the replaced character array; psrc++;} *pdes = ' n '; return p;} int main () {char s[] = "We are happy"; char s2[20];char *p;p = Instead (s, s2);p rintf ("%s", p); return 0;}

But if the interviewer asks for an operation on the original string and guarantees that the original string has enough space to hold the replaced string, we have to think of another method. If you replace the string after you go, the string that is saved after the space is bound to be overwritten, then we consider replacing it from the back.
1. First traverse the original string to find out the length of the string and the number of spaces within it. 2. Based on the length of the original string and the number of spaces we can find the length of the last new string. 3. Set two pointers point1 and Point2 point to the original string and the end position of the new string, respectively. 4. If point1 points to the content is not a space, then the content is assigned to the location pointed to Point2, if Point1 point to a space, then from the Point2 to assign the value "02%" 5. Until Point1==point2 indicates that all the spaces in the string have been replaced. code is as follows:
char* Instead1 (char *str, int sz,int len) {char *pstr = str;                 int blank_count = 0;                 int k;                 int point1, Point2;                                                while (*PSTR) {if (*pstr = = ")                                blank_count++;                pstr++;                } k = len + blank_count;                Point1 = Len;                 Point2 = k;                 Str[k + 1] = ' + ';  while (point1 >= 0 && point2 >= point1) {if (str [point1]! =                                 ") str[point2--] = str [point1--];                                                 else {str[point2--] = ' 0 ';                                            str[point2--] = ' 2 ';     str[point2--] = '% ';                                point1--; }} return str;}                 int main () {char s[20] = "We are happy";                Char *p;                p = Instead1 (s, sizeof (s)/sizeof (S[0]), strlen (s));                 printf ("%s", p); return 0;}

Bo Master Beginner Small white, welcome everyone to actively give advice, discuss together, progress together.

The sword refers to the offer analysis __ space substitution string problem

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.