Spaces in the string

1. Clear Space

       Write a program to string each space cleared. For example, enter "We are happy.", The output "Wearehappy.".

  • Programming ideas

        Since the memory allocation of the character string is allocated contiguously. When we remove a space character from a string of them, all the characters need to move forward behind a byte position. But if the latter movement requires each character string deleted, for the purposes of a string of length n, and a time to delete space characters complexity is O (n). For purposes of the present title number, it is possible to delete space characters is n, so this method is deleted in terms of time complexity of O (n- 2 ).

        In fact, we do not need to go all the characters move back one space at a time when deleted. We can imagine, when a space needs to be removed, and we put it occupies the position it behind the character to fill, it is equivalent to the spaces are removed. Specific methods are:

        (1) defines two pointers p1 and p2, p2 used to traverse the original string, p1 points to the result for non-space character string currently assigned. The initial state point to the first character string.

        (2) If p2 pointing element is not blank, then p2 point to the content assigned to p1, then the next element p1 and p2; if there p2 point to content space, then the next p2 point to an element, p1 remains stationary .

        (3) until p2 points to the end of the string "\ 0", cleared the end of the space.

  • Source code and operating results

#include<iostream>

using namespace std;

int main ()

{

    char str[81];

    cin.getline (str, 80, '\ n');   

    // set the pointer to the array's first two elements

    char *p1=str;

    char *p2=str;

    while(*p1!='\0')

    {

        if (* p2! = '') // if p2 does not point to a space, then copy the contents to p1 p2 point to point

            *p1++=*p2++;

        else // If p2 points to a space, then p2 pointer moves forward one space.

            p2++;

    }

    cout<<str<<endl;

    return 0;

}

 

        Example Delete the string all spaces, in practical applications, sometimes part of the reservation space required, as a plurality of strings of consecutive spaces, one is reserved. Thus, this problem can be extended, for a given string, and deleting the space at the end of the start, and the plurality of successive intermediate spaces merge into one.

2. Remove excess spaces

        Write a program for a given string, spaces and deleting at the beginning and end, and a plurality of successive intermediate spaces merge into one. For example, enter "We are happy.", The output "We are happy."

  • Programming ideas

        Because of the need to remove the opening spaces and at the end, but the middle of the space they need to save a string, hence the need for additional processing. Can be processed by setting a flag, bool define a variable flag indicating whether to save a space, if flage = true indicates a storage space, if the space is not saved flag = false. Initialized when the flag is set to false, so the beginning of the space will not be saved, not when it comes to a space character, save the character, and then set the flag = true show will be saved back to be scanned string the first space, so you can save when confronted with the first space.

       According to the above method of operation, after scanning the target at the end of the string or non-space character is a space character or, once determined like this, if the character is a space, the space which is set to "\ 0", If not for the space character, then it followed by "\ 0."

  • Source code and operating results

#include<iostream>

using namespace std;

int main ()

{

    char str[81];

    cin.getline (str, 80, '\ n');   

    int index=0;

    bool flag=false;

    for(int i=0;str[i]!='\0';i++)

    {

        if (str [i]! = '') // if the non-traversed space character, then the assignment

        {

            str[index++]=str[i];

            flag = true; // save a space representation allows

        }

        else if (flag) // If there is a space to allow

        {

            str[index++]=str[i];

            flag=false;

        }

    }

    if (index> 0 && str [index-1] == '') // processing of the last string extra spaces

    {

        str[index-1]='\0';

    }

    else

    {

        str[index]='\0';

    }

    cout<<str<<endl;

    return 0;

}

 

       Network programming, if the URL parameter containing special characters, such as space, "#" and the like, resulting in the server side does not recognize, put these special characters into a character can be recognized. Conversion rule is:% plus hexadecimal ASCII code, such as a space 32 is the ASCII code (hexadecimal 0x20), was replaced by 20%.

3. Replace spaces

      Writing a program, each of the spaces in the replacement string as "20%." For example, enter "We are happy.", The output "We% 20are% 20happy.".

  • Programming ideas

       The length of the space is replaced with a length of "20%" is 3, the length of the string becomes long. If another open up a new array to hold the replacement string space, then this problem is very easy to solve. Set two pointers point to the first string of old and new elements, traversing the original string, if you encounter a space to fill in the "% 20" in the new string, otherwise copy the contents of the original string. But this opened up a new array to hold the resulting string approach will result in a waste of space.

        If there is enough free space in the back of the original string, you can do replace the original string. Here to explore alternative methods.

       Because the space is replaced with the "20%", each replacement more than two characters, and therefore can count the total number and length of the original string which spaces, the calculated length of a string of "original string length + 2 * number of spaces. "

       Replacement operations back to front, the idea is: encountered non-space, directly behind the move; encounter spaces replaced with "% 20." To be inserted until the scanning position pointer and a pointer coincides with the location of the original string. The specific process is described as:

      (1) first traverse original string str, strlen statistics of the original length of the string, and wherein the number of spaces blanknum.

      (2) the number and length of the original string of blanks, obtained newlen result string length, i.e. newlen = strlen + blanknum * 2.

      (3) defines two pointers p1 and p2 point to the end position of the original string and the result string, i.e., p1 = str + strlen, p2 = str + newlen.

      (4) If p1 pointing to the content is not a space, then the content directly assigned to the location pointed to by p2, and p2 pointer forward; if p1 points to the contents of a space, then begin assigning points to the location from p2 "02 percent."

      (5) p1 pointer forward.

      (6) until p1 == p2, it indicates that all white space characters have been replaced finished.

      For example, according to the above operation, the string "Hello world" spaces in replacement operation shown in Fig.

FIG replacement operation space 1

  • Source code and operating results

#include<iostream>

using namespace std;

int main ()

{

    char str[81];

    cin.getline (str, 80, '\ n');   

    int strlen=0;

    int blanknum=0;

    int i=0;

    // string length and the number of spaces required

    while(str[i]!='\0')

    {

        strlen++;

        if(str[i]==' ')

            blanknum++;

        i++;

    }

    int newlen = strlen + blanknum * 2; // string length Innovation

    if(newlen>=80)

    {

        cout << "After the replacement string exceeds a predefined length." << endl;

              return 0;

       }

    // set two old and new pointer to the end of the array

    char *p1,*p2;

       p1=str+strlen;       p2=str+newlen;

    // When the above two pointers point to the same element it indicates that there are no spaces

    while(p1>=str && p2>p1)

    {

        if(*p1==' ')

        {

            *p2='0'; p2--;

            *p2='2'; p2--;

            *p2='%'; p2--;

        }

        else

        {

            *p2=*p1;  p2--;

        }

        p1--;

    }

    cout<<str<<endl;

    return 0;

}

 

Guess you like

Origin www.cnblogs.com/cs-whut/p/10990434.html