#include<stdio.h>
#include<string.h>
#include <stdlib.h>
void conventStr(char *str, const int strlen);
int main(void)
{
char str[] = "how old are you?";
conventStr(str, sizeof(str));
printf("%s", str);
system("pause");
//system("pause");
}
void conventStr(char *str,const int strlength)
{
char *newStr = (char *)malloc(strlength);
int index = strlength;
int newStrIndex = 0;
memset(newStr, ' ', strlength);
for (int i = strlength - 1; i >= 0; i--)
{
if ((str[i] == '?') || (str[i] == '\0'))
{
index = i;
newStr[i] = *(str + i);
}
if (str[i] == ' ')
{
memcpy(newStr + newStrIndex, str + i +1, index - i -1);//复制单词
newStrIndex += index - i; //新的存储数组索引,指示保存到哪里了
newStr[newStrIndex - 1] = ' '; //空格加到单词后面
index = i;
}
if (i == 0)
{
memcpy(newStr + newStrIndex, str, index - i);//复制单词
}
}
memcpy(str, newStr, strlength);
free(newStr);
}