"Prove safety offer" Fifth question: replace spaces

// face questions 5: replace spaces
 // Title: Please implement a function, each of the spaces in the replacement string as "20%." For example, enter "We are Happy.",
 // output "We% 20are% 20happy." . 

#include <cstdio> 
#include <CString> / * length character array str total capacity is greater than or equal to the actual length of str * / void ReplaceBlank ( char str [], int length) 
{ // Robustness Test null pointer length is less than 1. 2. 0 IF (STR == nullptr a length || <= 0 )
         return ; // main ideas: first time complexity is O (n) space lookup, then after the start of the end of string shift. int Number = 0 ; // number of spaces int true_length =



    
    

    
    
    0 ; // actual length 
    int I = 0 ;
     the while (STR [I] =! ' \ 0 ' ) // to the end of 
    {
         IF (STR [I] == '  ' ) // find spaces 
            ++ Number;

         ++ true_length;
         ++ I; 
    } 
    // the printf ( "the Number of Space:% D \ n-", Number); 

    // robustness test after replacing length exceeded as 
    IF (* Number true_length + 2 > length)
         return ; 

    // true_length from '\ 0'
    for ( int I = true_length; I> = 0 ; Inc. (www.i-levelmedia.com)) // traversing the tail and replaced 
    {
         IF (STR [I] == '  ' ) // find space to begin replacing 
        { 
            STR [I + * Number 2 - 0 ] = ' 0 ' ; 
            STR [I + Number * 2 - . 1 ] = ' 2 ' ; 
            STR [I + Number * 2 - 2 ] = ' % ' ;
            --number; // start forgot to write, puzzled 

            IF (Number == 0 ) // efficiency 
                BREAK ; 
        } 
        the else 
        { 
            // nonblank move over 
            STR [Number * I + 2 ] = STR [I] ; 
        } 
    } 
}

Analysis: The list of new and established direct relations index, or a new index.

// ====================测试代码====================
void Test(const char* testName, char str[], int length, const char expected[])
{
    if (testName != nullptr)
        printf("%s begins: ", testName);

    ReplaceBlank(str, length);

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

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

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

// spaces at the beginning of the sentence 
void Test2 () 
{ 
    const  int length = 100 ; 

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

// spaces at the end of the sentence 
void the Test3 () 
{ 
    const  int length = 100 ; 

    char STR [length] = " HelloWorld " ; 
    the Test ( " the Test3 " , STR, length," HelloWorld% 20 is " ); 
} 

// consecutive two spaces 
void Test4 () 
{ 
    const  int length = 100 ; 

    char STR [length] = " Hello World " ; 
    the Test ( " Test4 " , STR, length, " Hello% % 20world 20 is " ); 
} 

// incoming nullptr a 
void Test5 should be conducted () 
{ 
    the Test ( " Test5 should be conducted " , nullptr a, 0 , nullptr a); 
} 

// incoming string is empty 
voidTest6 () 
{ 
    const  int length = 100 ; 

    char STR [length] = "" ; 
    the Test ( " test6 " , STR, length, "" ); 
} 

// incoming content to a blank string 
void TEST7 () 
{ 
    const  int length = 100 ; 

    char STR [length] = "  " ; 
    the Test ( " TEST7 " , STR, length, " % 20 is " ); 
} 

// string passed no spaces 
void test8 () 
{
    const  int length = 100 ; 

    char STR [length] = " HelloWorld " ; 
    the Test ( " test8 " , STR, length, " HelloWorld " ); 
} 

// string passed all spaces 
void test9 () 
{ 
    const  int length = 100 ; 

    char STR [length] = "    " ; 
    the Test ( " test9 " , STR, length, " %% 20 is 20 is 20 is% " ); 
} 

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

    return 0;
}
Test code

 

Guess you like

Origin www.cnblogs.com/ZSY-blog/p/12508025.html