翻转不包含字母的字符串

2021-08-09

1.题目描述

const str = '123abd3-adfz-34-akjkfaf';
function reverseStr(str) {
}
// 输出:321abd-3adfz-43-akjkfaf

2. 解答

function reverseStr(str){
    let res = "";
    let needReverse = "";
    function ReverseNotAlp(needReverse){
        for(let i=needReverse.length-1; i>=0; i--){
            res += needReverse[i];
        }
    }
    for(let i=0; i<str.length; i++){
        if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')){    // 碰到字母开始翻转 needReverse
            ReverseNotAlp(needReverse);
            res += str[i];
            needReverse = "";
        }
        else{
            needReverse += str[i];
        }
    }
    return res;
}

console.log(reverseStr('123abd3-adfz-34-akjkfaf'))