剑指offer42-翻转单词顺序和字符串的左旋转

翻转句子中的单词顺序,输入一个句子,翻转句子中的单词顺序,但是单个单词中的字母顺序不变,如“I am a student.”翻转后为“student. a am I”。翻转方式为,首先将整个句子当成是一个字符串进行翻转,得到“.tneduts a ma I”。然后,以空格作为分隔符,将每个单词分别进行翻转,得到“student. a am I”,而对于整个句子和单个单词的翻转,其翻转的方式是一样的

#include <iostream>
using namespace std;
//输入一个字符串的头和尾指针,将其中的所有字符进行翻转
void reversestr(char* strBegin, char* strEnd)
{
    while (strBegin < strEnd)
    {
        char tmpchar;
        tmpchar = *strBegin;
        *strBegin = *strEnd;
        *strEnd = tmpchar;
        strBegin++;
        strEnd--;
    }
}
void ReverseSentence(char* input)
{
    if (input==NULL)
    {
        return;
    }
    char* strBegin = input;
    char* strEnd = input;
    while (*strEnd!='\0')
    {
        strEnd++;
    }
    strEnd--;
    reversestr(strBegin, strEnd);     //整个句子作为一个字符串进行翻转
    while (*strBegin!='\0')
    {
        strEnd = strBegin;
        while (*strEnd != ' ' && *strEnd != '\0')
        {
            strEnd++;
        }
        strEnd--;
        reversestr(strBegin, strEnd);          //翻转单个单词
        strBegin = ++strEnd;
        while (*strBegin==' ')
        {
            strBegin++;
        }
    }

}
// ====================测试代码====================
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(int argc, char* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();

    return 0;
}

//将字符串中的前面一部分移到字符串的尾部,而将剩下的字符前移
//如“abcdefgh”,旋转3个字符后为“defghabc”

#include <iostream>
using namespace std;
char* RotateString(char* input,int n)
{
    if (input == NULL )
    {
        return NULL;
    }
    if (n == 0)
    {
        return input;
    }
    char* FirstPartBegin = input;
    char* FirstPartEnd = input + n - 1;
    char* SecondPartBegin = input + n;
    char* SecondPartEnd = input + n;
    while (*SecondPartEnd != '\0')
    {
        SecondPartEnd++;
    }
    SecondPartEnd--;
    //reversestr函数与前面的一样
    reversestr(FirstPartBegin, FirstPartEnd);
    reversestr(SecondPartBegin, SecondPartEnd);
    reversestr(FirstPartBegin, SecondPartEnd);
    return FirstPartBegin;
}

// ====================测试代码====================
void Test(char* testName, char* input, int num, char* expectedResult)
{
    if (testName != NULL)
        printf("%s begins: ", testName);

    char* result = RotateString(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(int argc, char* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弹指间LDL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值