#include<stdio.h>
#include <string.h>
void reverse(char *a)
{
//首先逆置整个字符串
char* start = a;
char* end = a + strlen(a) - 1;
char temp;
while (start < end)
{
temp = *end;
*end = *start;
*start = temp;
start++;
end--;
}
//对单个单词进行逆置
start = a;
while (*start != '\0')
{
char* word_start = start;
while (*start != ' ' && *start != '\0')
{
start++;
}
//注意此时start的位置在空格上
char* word_end = start-1;
while (word_start < word_end)
{
temp = *word_start;
*word_start = *word_end;
*word_end = temp;
word_start++;
word_end--;
}
//移动到下一个单词
while (*start == ' ')
{
start++;
}
}
}
int main()
{
char a[30] = "man what can i say mamba out";
reverse(a);
printf("%s\n", a);
}
以单词为单位的逆置输出
最新推荐文章于 2024-07-24 14:05:37 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)