C language: String Replacement space: implement a function to replace each space in the string with "% 20 ".

Source: Internet
Author: User

C language: String Replacement space: implement a function to replace each space in the string with "% 20 ".
From the past to the future: replace the original character based on the original one (of course, the premise is that there is sufficient space). If a space is replaced from the past to the next, it will inevitably overwrite the original character and cannot be implemented. Because the space is replaced with "% 20", and two more characters are replaced each time, the total number of spaces in the string can be calculated, then the size of the new array is "size of the original array + 2 * Space ". Process from the back to the front: When a non-space is encountered, move directly to the back, and replace the space with "% 20" until the position to be inserted pointer and the original array are the pointer coincidence position.

# Include <stdio. h> # include <string. h> int main () {char arr [] = "we are happy. "; int I = 0; int j = 0; int len = 0; int count = 0; len = strlen (arr); for (I = 0; I <len; I ++) {if (arr [I] = '') {count ++ ;}} I = len; j = 2 * count + len; while (I! = J & I> = 0) {if (arr [I] = '') {arr [j --] = '0 '; arr [j --] = '2'; arr [j --] = '%'; I --;} else {arr [j] = arr [I]; j --; I --;} len = strlen (arr); for (I = 0; I <len; I ++) {printf ("% c ", arr [I]);} printf ("\ n"); return 0 ;}

 

Related Article

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.