Interview question 4: replace and clear spaces

Source: Internet
Author: User
Document directory
  • 1.1 Analysis
  • 1.2 code example
  • 2.1 Analysis
  • 2.2 code example
  • 3.1 Analysis
  • 3.2 code example
1. Replace Spaces
Question: implement a function to replace each space in the string with "% 20 ". For example, if "We are happy." Is input, "We % 20are % 20happy." Is output .".
1.1 Analysis

Replace a space of 1 with "% 20" of 3, and the yield of the character difference increases. If we can open up a new array to store strings after spaces are replaced, this question is very simple. Set the two pointers to point to the first element of the new and old strings respectively, and traverse the original string. If a space is reached, enter "% 20" in the new string. Otherwise, the contents of the Meta string will be copied. However, if the interviewer requires operations on the original string and ensures that the original string has enough space to store the replaced string, we have to find another method.

If the string is replaced from the beginning to the end, the string stored after the space will be overwritten, so we will consider replacing it from the back to the front.

  1. First, traverse the original string to find the length of the string and the number of spaces in 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 originalindex and newindex to point to the end of the original string and new string respectively.
  4. If originalindex points to content that is not a space, the content is assigned to the position pointed to by newindex. If originalindex points to a space, the value "02%" is assigned from newindex"
  5. Until originalindex = newindex indicates that all spaces in the string have been replaced.
1.2 view code

# Include <iostream> # include <stdlib. h> using namespace STD; void replaceblank (char string [], int Len) {If (string = NULL | Len <= 0) return; int stringlen = 0; int blanknum = 0; int I = 0; // evaluate the length of the string and the number of spaces while (string [I]! = '\ 0') {++ stringlen; If (string [I] = '') ++ blanknum; ++ I ;}int newlength = stringlen + blanknum * 2; // obtain the length of the new string if (newlength> Len) return; // set the two pointers to the end of the New and Old arrays int originalindex = stringlen; int newindex = newlength; // when the above two pointers point to the same element, there is no space while (originalindex> = 0 & newindex> originalindex) {If (string [originalindex] = '') {string [newindex --] = '0'; string [newindex --] = '2'; string [newindex --] = '% ';} else {string [newindex --] = string [originalindex] ;}-- originalindex ;}} void main () {char test [100] = "we are happy. "; cout <test <Endl; // we are happy. replaceblank (test, 100); cout <test <Endl; // we are happy. system ("pause ");}

 

2. Clear Spaces
Question: implement a function to clear every space in the string. For example, if "We are happy." Is input, "wearehappy." Is output .".
2.1 Analysis

This question requires a clear space, so the string obtained after the space is cleared is shorter than the original string. If we clear the space from start to end, it will not overwrite the characters after the space, so we can clear it from the beginning.

  1. Set the initial status of the two pointers P1 and P2 to point to the first character of the string.
  2. If P2 points to an element that is not a space, the content indicated by P2 is assigned to P1, and P1 and P2 point to the next element. If P2 points to a space, p2 points to the next element.
  3. The space is cleared until P2 points to '\ 0' at the end of the string.
2.2 view code

# Include <iostream> # include <stdlib. h> using namespace STD; void replaceblank (char * s) {// set two pointers to the first element char * P1 = s; char * P2 = s; while ('\ 0 '! = * P1) {If (''! = * P2) // If P2 points to a non-space character, copy P2 points to the content and point to * P1 ++ = * P2 ++; else // If P2 points to a space, the P2 pointer moves one cell forward. P2 ++ ;}} void main () {char test [100] = "we are happy. "; cout <test <Endl; // we are happy. replaceblank (TEST); cout <test <Endl; // we are happy. system ("pause ");}
3. Clear unnecessary Spaces
Question: for a given string, delete the spaces at the beginning and end, and combine multiple consecutive spaces in the middle into one. For example, if "We are happy." Is input, "We are happy." Is output ."
3.1 Analysis

This topic is similar to the second question, where spaces need to be deleted to ensure that the length of the output string is less than or equal to the length of the original string. Then, you can traverse from the beginning to the end without fear that subsequent characters will be overwritten. We need to delete spaces at the beginning and end, and save one space in the middle of the string. We can set a flag for processing. Defining a bool variable keep_space indicates whether to save a space. If keep_space = true, it indicates to save a space. If keep_space = false, no space is saved. Set keep_space to false during initialization, so that spaces in the initial stage will not be saved. If a character that is not a space is encountered, save the character, then, keep_space = true indicates that the first space in the string will be saved, so that it can be saved when the first space is met.

In the string result, after the preceding steps, you can ensure that the end of the string is either a non-null character or a blank character, so that you can make a judgment. If it is a space character, set this space to '\ 0'. If it is not empty, add' \ 0' to it'

3.2 view code

# Include <iostream> # include <stdlib. h> using namespace STD; void replaceblank (char * Str) {int Index = 0; bool keep_space = false; // indicates whether space can be saved, the initial value of false is used to remove unnecessary spaces at the beginning of the string for (INT I = 0; STR [I]! = '\ 0'; I ++) {If (STR [I]! = '') // If the convenience is not a space character, assign the value {STR [index ++] = STR [I]; keep_space = true; // indicates that a space can be saved} else if (keep_space) // If a space is allowed {STR [index ++] = STR [I]; keep_space = false ;}} // process the extra spaces at the end of the string. After the preceding processing, the string can contain at most one space at the index-1 position. If (index> 0 & STR [index-1] = '') {STR [index-1] = '\ 0 ';} else {STR [Index] = '\ 0';} void main () {char test [] = "we are happy. "; cout <strlen (TEST) <Endl; cout <sizeof (TEST) <Endl; cout <test <Endl; // we are happy. replaceblank (TEST); cout <test; // we are happy. system ("pause ");}

 

 

 

 

 

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.