一、运行结果
二、源代码
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <assert.h>
//实现逆转函数;
void reverse(char* left, char* right)
{
//断言left和right都不能为空;
assert(left);
assert(right);
//循环逆转字母;
while (left < right)
{
//置换;
char tmp = *left;
*left = *right;
*right = tmp;
//变换置换位置;
left++;
right--;
}
}
int main()
{
//初始化变量值;
char arr[101] = { 0 };
//获取变量值;
gets(arr);
//调用逆转函数;
int len = strlen(arr);
//逆序整个字符串;
reverse(arr, arr + len - 1);
//逆序每个单词;
char* start = arr;
//循环调用函数;
while (*start)
{
//初始化变量值;
char* end = start;
//循环判断是否++;
while (*end != ' ' && *end != '\0')
{
end++;
}
//调用逆转函数;
reverse(start, end - 1);
//判断end是否为空;
if (*end != '\0')
{
end++;
}
//跳转初始位置到下一个单词;
start = end;
}
//打印结果;
printf("%s\n", arr);
return 0;
}