c语言英语单词逆序输出
时间: 2023-12-02 22:41:44 浏览: 118
以下是C语言实现单词逆序输出的代码示例:
```c
#include <stdio.h>
#include <string.h>
void reverse(char *begin, char *end) {
char temp;
while (begin < end) {
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
void reverseWords(char *s) {
char *word_begin = NULL;
char *temp = s;
while (*temp) {
if ((word_begin == NULL) && (*temp != ' ')) {
word_begin = temp;
}
if (word_begin && ((*(temp + 1) == ' ') || (*(temp + 1) == '\0'))) {
reverse(word_begin, temp);
word_begin = NULL;
}
temp++;
}
reverse(s, temp - 1);
}
int main() {
char s[] = "hello world"; printf("Original string: %s\n", s);
reverseWords(s);
printf("Reversed string: %s\n", s);
return 0;
}
```
运行结果为:
```
Original string: hello world
Reversed string: world hello
```
阅读全文