您的位置:首页 > 其它

算法题目---翻转单词顺序 VS 左旋转字符串

2017-08-08 09:08 405 查看
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。

include<iostream>

#include<stdio.h>

#include<string.h>

using namespace std;

void Reverse(char* pBegin,char* pEnd)

{

    if(pBegin == NULL || pEnd == NULL)

        return;

    while(pBegin < pEnd)

    {

        char temp = *pBegin;

        *pBegin = *pEnd;

        *pEnd = temp;

        pBegin++,pEnd--;

    }

}

char* ReverseSentence(char* pData)

{

    if(pData == NULL)

        return NULL;

    char* pBegin = pData;

    char* pEnd = pData;

    

    while(*pEnd != '\0')

        pEnd++;

    pEnd--;

    Reverse(pBegin,pEnd);

    

    pBegin = pEnd = pData;

    while(*pBegin != '\0')

    {

        if(*pBegin == ' ')

        {

            pBegin++;

            pEnd++;

        }

        else if(*pEnd == ' ' || *pEnd == '\0')

        {

            Reverse(pBegin,--pEnd);

            pBegin = ++pEnd;

        }

        else

        {

            pEnd++;

        }

    }

    

    return pData;    

}

void Test(char* testName, char* input, char* expectedResult)

{

    if(testName != NULL)

        printf("%s begins: ", testName);

    ReverseSentence(input);

    if((input == NULL && expectedResult == NULL)

        || (input != NULL && strcmp(input, expectedResult) == 0))

        printf("Passed.\n\n");

    else

        printf("Failed.\n\n");

}

void Test1()

{

    char input[] = "I am a student.";

    char expected[] = "student. a am I";

    Test("Test1", input, expected);

}

void Test2()

{

    char input[] = "Wonderful";

    char expected[] = "Wonderful";

    Test("Test2", input, expected);

}

void Test3()

{

    Test("Test3", NULL, NULL);

}

void Test4()

{

    Test("Test4", "", "");

}

void Test5()

{

    char input[] = "   ";

    char expected[] = "   ";

    Test("Test5", input, expected);

}

int main()

{

    Test1();

    Test2();

    Test3();

    Test4();

    Test5();

    return 0;

}

字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。

void Reverse(char* pBegin,char* pEnd)

{

    if(pBegin == NULL || pEnd == NULL)

        return;

    while(pBegin < pEnd)

    {

        char temp = *pBegin;

        *pBegin = *pEnd;

        *pEnd = temp;

        pBegin++,pEnd--;

    }

}

char* LeftRotateString(char* pStr,int n)

{

    if(pStr != NULL)

    {

        int nLength = static_cast<int>(strlen(pStr));

        if(nLength > 0 && n > 0 && n < nLength)

        {

            char* pFirstStart = pStr;

            char* pFirstEnd = pStr + n - 1;

            char* pSecondStart = pStr + n;    

            char* pSecondEnd = pStr + nLength -1;

            Reverse(pFirstStart,pFirstEnd);

            Reverse(pSecondStart,pSecondEnd);

            Reverse(pFirstStart,pSecondEnd);

        }

    }

    return pStr;

}

void Test(char* testName, char* input, int num, char* expectedResult)

{

    if(testName != NULL)

        printf("%s begins: ", testName);

    char* result = LeftRotateString(input, num);

    if((input == NULL && expectedResult == NULL)

        || (input != NULL && strcmp(result, expectedResult) == 0))

        printf("Passed.\n\n");

    else

        printf("Failed.\n\n");

}

void Test1()

{

    char input[] = "abcdefg";

    char expected[] = "cdefgab";

    Test("Test1", input, 2, expected);

}

void Test2()

{

    char input[] = "abcdefg";

    char expected[] = "bcdefga";

    Test("Test2", input, 1, expected);

}

void Test3()

{

    char input[] = "abcdefg";

    char expected[] = "gabcdef";

    Test("Test3", input, 6, expected);

}

void Test4()

{

    Test("Test4", NULL, 6, NULL);

}

void Test5()

{

    char input[] = "abcdefg";

    char expected[] = "abcdefg";

    Test("Test5", input, 0, expected);

}

void Test6()

{

    char input[] = "abcdefg";

    char expected[] = "abcdefg";

    Test("Test6", input, 7, expected);

}

int main()

{

    Test1();

    Test2();

    Test3();

    Test4();

    Test5();

    Test6();

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: