翻转单词顺序
何海涛:《剑指Offer:名企面试官精讲典型编程题》:九度OJ
题目描述:http://ac.jobdu.com/problem.php?cid=1039&pid=25student. a am II'm a Freshman and I like JOBDU!
I am a student.JOBDU! like I and Freshman a I'm
思想:举个例子大家就知道了!例如:输入 123 456 789
我们先将每个字串翻转 321 654 987
再将整体翻转 789 456 123 OK,效果达到!
其实最后一次的翻转都不需要了,直接从后往前输出字符就ok!
代码AC:
#include <stdio.h>#include <string.h>int main(){ char str[50001], ch; int i, low, high, tmp, len; while( gets( str ) ) { low = 0; high = 0; len = strlen( str ); while( low < len ) { while( str[low] == ' ' ) { low++; } high = low; while( str[high] ) { if( str[high] == ' ' ) { high--; break; } else { high++; } } if( str[high] == '\0' ) { high--; } tmp = high + 1; while( low < high ) { ch = str[low]; str[low] = str[high]; str[high] = ch; low++; high--; } low = tmp; high = tmp; } for( i = len - 1; i > 0; i-- ) { printf("%c", str[i]); } printf("%c\n", str[0]); } return 0;}