算法题目---翻转单词顺序 VS 左旋转字符串
来源:互联网 发布:钥匙扣定做厂家淘宝 编辑:程序博客网 时间:2024/05/16 08:40
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。
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;
}
- 算法题目---翻转单词顺序 VS 左旋转字符串
- 翻转单词顺序VS左旋转字符串
- 翻转单词顺序 VS 左旋转字符串
- 翻转单词顺序VS左旋转字符串
- 翻转单词顺序vs左旋转字符串
- 翻转单词顺序VS左旋转字符串
- 翻转单词顺序VS左旋转字符串----->“字符串翻转”
- 剑指Offer之 - 翻转单词顺序 VS 左旋转字符串
- 面试题42:翻转单词顺序VS左旋转字符串
- 42.翻转单词顺序VS左旋转字符串
- 面试题46:翻转单词顺序VS左旋转字符串
- 翻转单词顺序 VS 左旋转字符串43
- Q42:翻转单词顺序VS左旋转字符串
- 剑指offer 42. 翻转单词顺序VS左旋转字符串
- 面试题42:翻转单词顺序VS左旋转字符串
- 面试题42:翻转单词顺序 VS 左旋转字符串
- 面试算法(四十二)翻转单词顺序VS左旋转字符串
- 翻转单词序列 vs 左旋转字符串
- Android:学习AIDL,这一篇文章就够了(上)
- 两种rop and egghunter
- 动态规划中级教程 96. Unique Binary Search Trees
- 剑指offer-两个链表的第一个公共点
- Idea JSP随处断点,CSS行都OK
- 算法题目---翻转单词顺序 VS 左旋转字符串
- sql查询语句
- 获取剩余的秒数
- leetcode--Valid Parentheses
- JAVA--集合知识总结
- 回归
- react
- MyBatis mapper.xml配置文件
- 网络摄像机-ISP基础一