第一种方法:
#include < iostream >
#include < string >
using namespace std;
void Reverse(char *pBegin, char *pEnd)
{
//判断字符串是否长度大于1。
if (pBegin == pEnd)
return;
//判断交换的条件。
while (pBegin < pEnd)
{
//实现交换
char tmp = *pBegin;
*pBegin = *pEnd;
*pEnd = tmp;
pBegin++;
pEnd--;
}
}
void ReverseSentence(char *pSentence)
{
//判断句子是否为空。
if ( strlen(pSentence)==0 )
return;
Reverse(pSentence, pSentence+strlen(pSentence)-1);
char *pBegin = pSentence;
char *pEnd = pSentence;
//判断句末的条件。
while (*pEnd != '\0')
{
if (*pEnd != ' ')
pEnd++;
else
{
Reverse(pBegin, pEnd-1);
pBegin = ++pEnd;
}
}
Reverse(pBegin, pEnd-1);
}
int main()
{
char str[] = { "you are successful" };
cout << str << endl;
ReverseSentence(str);
cout << str << endl;
return 0;
}
第二种方法
#include <iostream>
#include <string>
using namespace std;
int main()
{
char a[] = "you are successful";
cout << a << endl;
char *b[5];
char *c;
c = strtok(a, " ");
int i = 0;
while(c)
{
b[i++] = c;
c = strtok(NULL, " ");
}
for(int j=i-1; j>=0; j--)
cout << b[j] << " ";
cout << endl << endl << endl;
return 0;
}