简体   繁体   中英

Reverse the word in a string with the same order in javascript without using the array functions except .length

I want reverse the string with the same order. We should not use the Array functions like split(), .reverse() and .join() . but we can use array.length .

Here i am attached my code. How to achieve that thing more efficiently.

var str = "i am javascript";
// result "i ma tpircsavaj"
function reverseString(myStr){
    var strlen = myStr.length, result = "", reverseStr = "", reverseStrArr = [];
  for(var i = strlen-1; i >= 0; i--){
    reverseStr +=  myStr[i];
  }

    for(var j = 0; j < strlen; j++){
    if(reverseStr[j] == " "){
      reverseStrArr.push(result);
      result = "";
    }else{
      result += reverseStr[j];
      if(j + 1 == strlen){
        reverseStrArr.push(result);
        result = "";
      }
    }
  }

  for(var k=reverseStrArr.length - 1; k >= 0; k--){
    result += reverseStrArr[k] + " "
  }
  console.log(result);
}
reverseString(str);

Use 2 pointers: i to indicate the current position and j to indicate the start index of the current word. Add the reversed of current word char by char when space.

Don't be fooled by the nested loops, the complexity is the same as yours: O(n) and somehow cleaner for me.

 var string = "i love javascript and the whole world!" var result = "" var i = j = 0 var l = string.length while (i++ < l) { var k = i if (string[i] === " " || (i === l - 1) && k++) { while (--k >= j) result += string[k] j = i + 1 result += " " } } result = result && result.slice(0, -1) || "" console.log(result) 

 function reverseStr(str) { var ret = ""; for (var i = 0; i < str.length; i++) { ret = str[i] + ret; } return ret; } function doIt(str) { var ret = "", cur = ""; for (var i = 0; i < str.length; i++) { var c = str.charAt(i); if (c == ' ' || c == '.') { ret += reverseStr(cur) + c; cur = ""; } else { cur += c; } } ret += reverseStr(cur); return ret; } console.log(doIt('Reverse the word in a string with the same order in javascript without using the array functions except .length')); 

You could take a loop, collect the characters of a word and reverse the word.

 function reverse(string) { var reversed = ''; while (reversed.length !== string.length) { reversed = string[reversed.length] + reversed; } return reversed; } var string = "i am javascript", temp = '', result = '', i = 0; while (i < string.length) { if (string[i] === ' ') { result += (result && ' ') + reverse(temp); temp = ''; } else { temp += string[i]; } i++; } if (temp) result += (result && ' ') + reverse(temp); console.log(result); 

An approach from the end.

 function reverse(string) { var reversed = ''; while (reversed.length !== string.length) { reversed = string[reversed.length] + reversed; } return reversed; } var string = "i am javascript", temp = '', result = '', i = string.length; while (i--) { if (string[i] === ' ') { result = ' ' + reverse(temp) + result; temp = ''; } else { temp = string[i] + temp; } } if (temp) result = reverse(temp) + result; console.log(result); 

 function reverse(word) { if (word.length > 1) { var newWord = ''; for (j = word.length-1; j >= 0; j--) { newWord += word[j]; } return newWord + ' '; } else { return word + ' '; } } var text = "i am javascript"; var words = text.split(' '); var result = ''; for (i = 0; i < words.length; i++) { result += reverse(words[i]); } console.log(result); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM