#include <string.h>
#include <exception>
void Reverse(char list[],int begin,int end)
{
if (begin > end)
{
throw new std::exception("error");
}
char temp;
while(begin < end)
{
temp = list[begin];
list[begin] = list[end];
list[end] = temp;
begin++;
end--;
}
}
void ReverseStr(char list[])
{
if (list == NULL)
{
throw new std::exception("empty string");
}
int len = strlen(list);
Reverse(list,0,len-1);
int i = 0;
int begin = i;
int end;
do
{
i++;
if (list[i] == ' ' || list[i] == '\0')
{
end = i-1;
Reverse(list,begin,end);
begin = i +1;
}
}while(list[i] != '\0');
}
int main()
{
char list[] = "hi welcome to mogujie";
printf("%s\n",list);
ReverseStr(list);
printf("%s\n",list);
//ReverseStr(NULL);
return 0;
}
输出结果
hi welcome to mogujie
mogujie to welcome hi