"the sky is blue" 反转后为 "blue is the sky"
实现思路:
对英文句子每一个字符做循环
s存放反转的句子。w存放单词
遇到空格(1个或多个)将w加入到s中
非空格则将字符加入到w
实现代码:
var reverseWords = function(str) { str = str.trim(); var s = ""; var len = str.length; var w = ""; for(var i = len-1;i>=0;i--){ if(str[i] != " "){ w = str[i] + w; } else{ if(i != 0 && str[i-1] == " ") { continue; } s += w + " " w=""; } } s += w; return s; };