[C language brushing questions] Niuke.com brushing questions - replace spaces


topic description

Please implement a function to replace every space in a string with "%20". For example, when the string is We Are Happy. Then the string after replacement is We%20Are%20Happy.

Core code pattern:

void replaceSpace(char *str,int length)
{
    
    

}

The title here is to directly modify the string pointed to by str, and replace each space with "%20" as required, and length is the length of the given string.

Idea analysis and code implementation (C language)

1. Double pointer shift method

We don't create a new array, and directly "modify" the string pointed to by str. We assume that we want to add a section to the original string, define two pointers, end1 points to the end of the current string, and end2 points to the end of the increased string, so how much should we increase? You think, the title says that each space should be replaced with "%20", that is, the original one character becomes three characters, so every time the string is changed, it will increase by 2 bytes, and there are several spaces Just change it a few times, so in the end it will increase the number of spaces by 2. How to get the number of spaces is actually very simple, just traverse the string and count the spaces.
Let's take the example "We Are Happy." given in the title to explain.
Replace spaces illustration 1.png
Next, let the two pointers move forward. If end1 does not encounter a space, copy the character pointed to by end1 to the position pointed to by end2. If end1 encounters a space, let the point of end2 start from the current position and move forward. Enter the characters '0', '2' and '%'. Since these processes need to be repeated, we can easily know that a loop needs to be applied, so what is the loop condition?
During the forward movement of the two pointers, end1 is fixed at -1 in each cycle, but end2 is different. If end1 does not encounter a space, end2 moves forward one bit in each round like end1; if end1 encounters a space, then At this time, end2 moves forward three digits (because "%20" is to be put in), so it moves two digits forward more than end1, so it needs to move forward by 2
space digits in total, and end1 is closer than end2 at the beginning The first 2*space digits, that is to say, the two pointers will eventually meet, when they meet, it means that the shifting is over and the string modification is completed. So the condition of the loop is end1 != end2.
Replace spaces illustration 2.png
Replace spaces diagram 3.png
Hey, when end1 and end2 are the same, "We" will not be shifted. Is there a problem? Don't worry, look at the picture carefully. At this time, "We" is the first word and there is no need to shift it anymore!

Code

void replaceSpace(char* str, int length)
{
    
    
    int space_cnt = 0;
    char* tmp = str;
    while (*tmp)
    {
    
    
        if (*tmp == ' ')
            space_cnt++;
        tmp++;
    }

    char* end1 = str + length - 1;
    char* end2 = end1 + 2 * space_cnt;

    while (end1 != end2)
    {
    
    
        if (*end1 != ' ')
        {
    
    
            *end2-- = *end1--;
        }
        else
        {
    
    
            end1--;
            *end2-- = '0';
            *end2-- = '2';
            *end2-- = '%';
        }
    }
}

2. Auxiliary array traversal copy

Consider creating a new auxiliary array, which is larger than the original array. Here we directly double the size and dynamically open up memory. Traverse the original array, if there is no space, copy the characters intact to the auxiliary array, if there is a space, put "%20" into the auxiliary array in order, the subscript of the auxiliary array is +3, after all Three characters are put at a time. After traversing, remember to add a '\0' to the auxiliary array at the end.
Finally, copy the auxiliary array to the original array.

Code

void replaceSpace(char* s, int len)
{
    
    
    int cnt = 0;
    char* ret = (char*)calloc(2 * len, sizeof(char));
    char* ptr = s;

    while (*ptr)
    {
    
    
        if (*ptr != ' ')
        {
    
    
            ret[cnt] = *ptr;
            cnt++;
        }
        else
        {
    
    
            ret[cnt] = '%';
            ret[cnt + 1] = '2';
            ret[cnt + 2] = '0';
            cnt += 3;
        }
        ptr++;
    }
    ret[cnt] = '\0';
    strcpy(s, ret);
}

insert image description here

Guess you like

Origin blog.csdn.net/weixin_61561736/article/details/126092332
Recommended