题目:将一句话的单词倒置,标点不倒置。比如:I like beijing. 经过函数后变为:beijing. like I
输入描述:输入用例长度不超过100
输出描述:依次输出倒置之后的字符串,以空格分割
解题思路:
I like beijing.
I ekil .gnijieb? ? ? 将每个单词一一逆序
beijing. like I? ? ? 将整个字符串逆序
举个简单的例子:abc def hi.
#include<stdio.h>
#include<assert.h>
#include<string.h>
void reverse(char* left, char* right)
{
assert(left&&right); //断言 避免传进来的left和right是空指针
while (left<right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
int main()
{
char arr[100] = { 0 };
gets(arr);
char* start = arr;
char* end = start;
int len = strlen(arr);
while (*end != '\0')
{
while (*end != ' '&& *end != '\0')
{
end++;
}
reverse(start, end - 1);
if (*end == '\0')
start = end;
else
start = end + 1;
end = start;
}
reverse(arr, arr + len - 1);
printf("%s\n", arr);
return 0;
}
运行结果:
|