给定一个英文句子,翻转句子中单词的顺序。要求单词内字符的顺序不变,句子中单词以空格符隔开。标点符号和普通字母一样处理。例如,“I am a student.”翻转为“student. a am I”
思路:先将句子完全翻转,再将单词反转
#include <iostream>
#include <stdio.h>
int main( )
{
int i=0, j=0, begin, end;
char str[] = "I am a student.", temp;
j = strlen(str)-1;
printf("string = %s\n", str);
//1、全盘反转
while(j > i)
{
temp = str[i];
str[i++] = str[j];
str[j--] = temp;
}
printf("string = %s\n", str);
//2、部分反转
i = 0;
while(str[i])
{
if(str[i] != ' ')
{
begin = i;
while(str[i] && str[i] != ' ')
{
i++;
}
i = i - 1;
end = i;
}
while(end > begin)
{
temp = str[begin];
str[begin++] = str[end];
str[end--] = temp;
}
i++;
}
printf("string = %s\n", str);
return 0;
}