reverse_all_part_words
来源:互联网 发布:进销存着淘宝 编辑:程序博客网 时间:2024/06/05 06:38
#include <stdio.h>#include <string.h>void reverse(char str[], int beg, int end){ char tmp; while (beg < end) { tmp = str[beg]; str[beg] = str[end]; str[end] = tmp; ++beg; --end; } }int main(){ char str[] = "apple one a student hello world"; int beg = 0; int end = strlen(str) - 1; int cur; printf("reverse all : "); reverse(str, beg, end); puts(str); beg = 0; cur = 0; while (str[cur] != '\0') { ++cur; if (str[cur] == ' ') { --cur; reverse(str + beg, 0, cur - beg); beg = cur + 2; cur = beg; } } if (str[cur] == '\0') { --cur; reverse(str + beg, 0, cur - beg); } printf("reverse part: "); puts(str); return 0;}
0 0