替換空格


 

題目:請實現一個函數,把字符串中的每個空格替換成“%20”。例如輸入“We are happy.”,則輸出“We%20are%20happy.”。

      看到這個題目,我們首先應該想到的是原來一個空格字符,替換之后變成'%'、'2'和'0'這3個字符,因此字符串會變長。如果是在原來的字符串上做替換,那么就有可能覆蓋修改在該字符串后面的內存。如果是創建新的字符串並在新的字符串上做替換,那么我們可以自己分配足夠多的內存。

      在這里介紹一種時間復雜度為O(n)的解法。 

      我們可以先遍歷一次字符串,這樣就能統計出字符串中空格的總數,並可以由此計算出替換之后的字符串的總長度。每替換一個空格,長度增加2,因此替換以后字符串的長度等於原來的長度加上2乘以空格數目。我們以字符串"We are happy."為例,"We are happy."這個字符串的長度是14(包括結尾符號'\0'),里面有兩個空格,因此替換之后字符串的長度是18。

      我們從字符串的后面開始復制和替換。首先准備兩個指針,P1和P2。P1指向原始字符串的末尾,而P2指向替換之后的字符串的末尾(如圖(a)所示)。接下來我們向前移動指針P1,逐個把它指向的字符復制到P2指向的位置,直到碰到第一個空格為止。此時字符串包含如圖(b)所示,灰色背景的區域是做了字符拷貝(移動)的區域。碰到第一個空格之后,把P1向前移動1格,在P2之前插入字符串"%20"。由於"%20"的長度為3,同時也要把P2向前移動3格如圖(c)所示。

     我們接着向前復制,直到碰到第二個空格(如圖(d)所示)。和上一次一樣,我們再把P1向前移動1格,並把P2向前移動3格插入"%20"(如圖(e)所示)。此時P1和P2指向同一位置,表明所有空格都已經替換完畢

注:圖中帶有陰影的區域表示被移動的字符。(a)把第一個指針指向字符串的末尾,把第二個指針指向替換之后的字符串的末尾。(b)依次復制字符串的內容,直至第一個指針碰到第一個空格。(c)把第一個空格替換成'%20',把第一個指針向前移動1格,把第二個指針向前移動3格。(d)依次向前復制字符串中的字符,直至碰到空格。(e)替換字符串中的倒數第二個空格,把第一個指針向前移動1格,把第二個指針向前移動3格。

參考代碼:

 1 /*length 為字符數組string的總容量*/
 2 void ReplaceBlank(char string[], int length)
 3 {
 4     if(string == NULL && length <= 0)
 5         return;
 6  
 7     /*originalLength 為字符串string的實際長度*/
 8     int originalLength = 0;
 9     int numberOfBlank = 0;
10     int i = 0;
11     while(string[i] != '\0')
12     {
13         ++ originalLength;
14  
15         if(string[i] == ' ')
16             ++ numberOfBlank;
17  
18         ++ i;
19     }
20  
21     /*newLength 為把空格替換成'%20'之后的長度*/
22     int newLength = originalLength + numberOfBlank * 2;
23     if(newLength > length)
24         return;
25  
26     int indexOfOriginal = originalLength;
27     int indexOfNew = newLength;
28     while(indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
29     {
30         if(string[indexOfOriginal] == ' ')
31         {
32             string[indexOfNew --] = '0';
33             string[indexOfNew --] = '2';
34             string[indexOfNew --] = '%';
35         }
36         else
37         {
38             string[indexOfNew --] = string[indexOfOriginal];
39         }
40  
41         -- indexOfOriginal;
42     }
43 }

 

 完整代碼:

 1 #include<iostream>
 2 #include<string.h>
 3 using namespace std;
 4  
 5 /*length 為字符數組string的總容量*/
 6 void ReplaceBlank(char string[], int length)
 7 {
 8     if(string == NULL && length <= 0)
 9         return;
10  
11     /*originalLength 為字符串string的實際長度*/
12     int originalLength = 0;
13     int numberOfBlank = 0;
14     int i = 0;
15     while(string[i] != '\0')
16     {
17         ++ originalLength;
18  
19         if(string[i] == ' ')
20             ++ numberOfBlank;
21  
22         ++ i;
23     }
24  
25     /*newLength 為把空格替換成'%20'之后的長度*/
26     int newLength = originalLength + numberOfBlank * 2;
27     if(newLength > length)
28         return;
29  
30     int indexOfOriginal = originalLength;
31     int indexOfNew = newLength;
32     while(indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
33     {
34         if(string[indexOfOriginal] == ' ')
35         {
36             string[indexOfNew --] = '0';
37             string[indexOfNew --] = '2';
38             string[indexOfNew --] = '%';
39         }
40         else
41         {
42             string[indexOfNew --] = string[indexOfOriginal];
43         }
44  
45         -- indexOfOriginal;
46     }
47 }
48  
49 int main()
50 {
51     const int length = 100;
52  
53     char string[length] = "hello world.";
54  
55     ReplaceBlank(string, length);
56  
57     int newLength = strlen(string);
58 /*
59     int newLength = 0;
60     int j = 0;
61     while(string[j] != '\0')
62     {
63         newLength++;
64         ++j;
65     }
66 */
67     for(int i = 0 ; i < newLength ; ++i)
68         cout<<string[i];
69     cout<<endl;
70  
71     return 0;
72 }

 

 

測試代碼:

#include <stdio.h>
#include <string>

/*length 為字符數組string的總容量*/
void ReplaceBlank(char string[], int length)
{
    if(string == NULL && length <= 0)
        return;

    /*originalLength 為字符串string的實際長度*/
    int originalLength = 0;
    int numberOfBlank = 0;
    int i = 0;
    while(string[i] != '\0')
    {
        ++ originalLength;

        if(string[i] == ' ')
            ++ numberOfBlank;

        ++ i;
    }

    /*newLength 為把空格替換成'%20'之后的長度*/
    int newLength = originalLength + numberOfBlank * 2;
    if(newLength > length)
        return;

    int indexOfOriginal = originalLength;
    int indexOfNew = newLength;
    while(indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
    {
        if(string[indexOfOriginal] == ' ')
        {
            string[indexOfNew --] = '0';
            string[indexOfNew --] = '2';
            string[indexOfNew --] = '%';
        }
        else
        {
            string[indexOfNew --] = string[indexOfOriginal];
        }

        -- indexOfOriginal;
    }
}

void Test(char* testName, char string[], int length, char expected[])
{
    if(testName != NULL)
        printf("%s begins: ", testName);

    ReplaceBlank(string, length);

    if(expected == NULL && string == NULL)
        printf("passed.\n");
    else if(expected == NULL && string != NULL)
        printf("failed.\n");
    else if(strcmp(string, expected) == 0)
        printf("passed.\n");
    else
        printf("failed.\n");
}

// 空格在句子中間
void Test1()
{
    const int length = 100;

    char string[length] = "hello world";
    Test("Test1", string, length, "hello%20world");
}

// 空格在句子開頭
void Test2()
{
    const int length = 100;

    char string[length] = " helloworld";
    Test("Test2", string, length, "%20helloworld");
}

// 空格在句子末尾
void Test3()
{
    const int length = 100;

    char string[length] = "helloworld ";
    Test("Test3", string, length, "helloworld%20");
}

// 連續有兩個空格
void Test4()
{
    const int length = 100;

    char string[length] = "hello  world";
    Test("Test4", string, length, "hello%20%20world");
}

// 傳入NULL
void Test5()
{
    Test("Test5", NULL, 0, NULL);
}

// 傳入內容為空的字符串
void Test6()
{
    const int length = 100;

    char string[length] = "";
    Test("Test6", string, length, "");
}

//傳入內容為一個空格的字符串
void Test7()
{
    const int length = 100;

    char string[length] = " ";
    Test("Test7", string, length, "%20");
}

// 傳入的字符串沒有空格
void Test8()
{
    const int length = 100;

    char string[length] = "helloworld";
    Test("Test8", string, length, "helloworld");
}

// 傳入的字符串全是空格
void Test9()
{
    const int length = 100;

    char string[length] = "   ";
    Test("Test9", string, length, "%20%20%20");
}

int main(int argc, char* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
    Test7();
    Test8();
    Test9();

    return 0;
}

 

 

延伸題目:歸並兩個已經排序的數組。

題目:有兩個排序的數組A1和A2,內存在A1的末尾有足夠多的空余空間容納A2。請實現一個函數,把A2中所有的數字插入到A1中並且所有的數字是排序的。

void mergaMatrix(int* matrix1,int* matrix2,  
                 int lenofmtrx1,int lenofmtrx2,int sizeofmatrix1)  
{  
    if(sizeofmatrix1 != 0 && matrix1 != NULL && lenofmtrx1 !=0   
        &&  matrix2 != NULL && lenofmtrx2 != 0 )   
    {  
        int* pNewMatrix1 = matrix1 + lenofmtrx1 + lenofmtrx2 -1;  
        int* pMatrix1 = matrix1 + lenofmtrx1 - 1;  
        int* pMatrix2 = matrix2 +lenofmtrx2 - 1;  
  
        while(pMatrix1 >= matrix1 && pMatrix2 >= matrix2)  
        {  
            if(*pMatrix1 >= *pMatrix2)  
                *pNewMatrix1-- = *pMatrix1--;  
            else  
                *pNewMatrix1-- = *pMatrix2--;  
        }  
        while(pMatrix1 >= matrix1)  
        {  
            *pNewMatrix1-- = *pMatrix1--;  
        }  
        while(pMatrix2 >= matrix2)  
        {  
            *pNewMatrix1-- = *pMatrix2--;  
        }  
    }  
    return;  
}  

 測試代碼:

//合並數組  
#include <stdio.h>  
void mergaMatrix(int* matrix1,int* matrix2,  
                 int lenofmtrx1,int lenofmtrx2,int sizeofmatrix1)  
{  
    if(sizeofmatrix1 != 0 && matrix1 != NULL && lenofmtrx1 !=0   
        &&  matrix2 != NULL && lenofmtrx2 != 0 )   
    {  
        int* pNewMatrix1 = matrix1 + lenofmtrx1 + lenofmtrx2 -1;  
        int* pMatrix1 = matrix1 + lenofmtrx1 - 1;  
        int* pMatrix2 = matrix2 +lenofmtrx2 - 1;  
  
        while(pMatrix1 >= matrix1 && pMatrix2 >= matrix2)  
        {  
            if(*pMatrix1 >= *pMatrix2)  
                *pNewMatrix1-- = *pMatrix1--;  
            else  
                *pNewMatrix1-- = *pMatrix2--;  
        }  
        while(pMatrix1 >= matrix1)  
        {  
            *pNewMatrix1-- = *pMatrix1--;  
        }  
        while(pMatrix2 >= matrix2)  
        {  
            *pNewMatrix1-- = *pMatrix2--;  
        }  
    }  
    return;  
}  
  
//單元測試  
void test(int* matrix1,int* matrix2,  
          int lenofmtrx1,int lenofmtrx2,int sizeofmatrix1)  
{  
    if(matrix1 != NULL)  
    {  
        for( int i=0; i<lenofmtrx1;i++)  
        {  
            printf("%d ",*(matrix1+i));  
        }  
    }  
    printf("\n");  
    if(matrix2 != NULL){  
        for( int i=0; i<lenofmtrx2;i++)  
        {  
            printf("%d ",*(matrix2+i));  
        }  
    }  
    printf("\n");  
    mergaMatrix(matrix1,matrix2,lenofmtrx1,lenofmtrx2,sizeofmatrix1);  
    for( int i=0; i<lenofmtrx1+lenofmtrx2;i++)  
    {  
        printf("%d ",*(matrix1+i));  
    }  
    printf("\n");  
}  
//一般情況  
void test1()  
{  
    const int sizeofmatrix1 = 100;  
    int lenofmtrx1 = 3;  
    int matrix1[sizeofmatrix1] = {1,3,5};  
    int lenofmtrx2 = 4;  
    int matrix2[] = {2,4,6,8};  
    test(matrix1,matrix2,lenofmtrx1,lenofmtrx2,sizeofmatrix1);  
}  
//其中一個數組的書全部小於另外一個  
void test2()  
{  
    const int sizeofmatrix1 = 100;  
    int lenofmtrx1 = 3;  
    int matrix1[sizeofmatrix1] = {1,3,5};  
    int lenofmtrx2 = 4;  
    int matrix2[] = {6,7,8,9};  
    test(matrix1,matrix2,lenofmtrx1,lenofmtrx2,sizeofmatrix1);  
}  
//其中一個為空  
void test3()  
{  
    const int sizeofmatrix1 = 100;  
    int lenofmtrx1 = 3;  
    int matrix1[sizeofmatrix1] = {1,3,5};  
    test(matrix1,NULL,lenofmtrx1,0,sizeofmatrix1);  
}  
//兩個都為空  
void test4()  
{  
    const int sizeofmatrix1 = 100;  
    test(NULL,NULL,0,0,sizeofmatrix1);  
}  
int main()  
{  
    test1();  
    test2();  
    test3();  
    test4();  
    return 0;  
}  

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM