比如一个字符串“this is a test.”,将其反转后为"test. a is this"。
#include <stdio.h>
#include <string.h>
void ReverseString(char str[],int start,int end)
{
if (NULL == str || start > end)
{
return;
}
char c = ' ';
while (start < end)
{
c = str[start];
str[start] = str[end];
str[end] = c;
start++;
end--;
}
}
int main()
{
char str[] = "this is a test.";
int i = 0;
int j = 0;
int iLen = strlen(str);
ReverseString(str,0,iLen-1);
printf("%s\n",str);
for (; i <= iLen; i++)
{
if (str[i] == ' ' || str[i] == '\0')
{
ReverseString(str,j,i-1);
j = i+1;
}
}
printf("%s\n",str);
}