简体   繁体   English

在javascript中以相同顺序颠倒字符串中的单词,而不使用数组函数(.length除外)

[英]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() . 我们不应该使用Array函数,例如split(), .reverse() and .join() but we can use array.length . 但是我们可以使用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. 使用2个指针:i指示当前位置,j指示当前单词的起始索引。 Add the reversed of current word char by char when space. 空格时,将当前单词char的反义字符逐个字符添加。

Don't be fooled by the nested loops, the complexity is the same as yours: O(n) and somehow cleaner for me. 不要被嵌套循环所迷惑,复杂度与您的相同:O(n)对我来说更干净。

 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); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 反转字符串中每个单词的字母顺序 - JavaScript reverse the order of letters for each word in a string 如何在不使用javascript中任何内置函数的情况下反转字符串 - how to reverse string without using any built in functions in javascript 如何使用 javascript 在字符串中逐字反转? - How to reverse word by word in a string using javascript? javascript 在不使用 reverse() 的情况下反转数组 - javascript reverse an array without using reverse() Javascript Array String Word Wrap Problem ---按顺序排列字符串和给定数组长度 - Javascript Array String Word Wrap Problem --- Permutation of Strings in Order and A Given Array Length 每个单词的 Javascript 字符串反向 - Javascript string reverse for each word 当字符串中单词的字符长度小于数组中字符的长度时,如何根据Javascript中的数组删除字符串中的单词? - How to remove word in string based on array in Javascript when word's character length in string is fewer than in array? 将字长存储在javascript数组中 - Storing the word length in javascript array 不使用reverse()方法。 如何保持反向的原始字符串顺序,空格和标点符号? - Without using the reverse() method. How do I maintain the original string order, space and punctuation on string that was reverse? 如何在不使用本机长度方法的情况下在 javascript 中获取字符串的长度 - How to get length of string in javascript without using native length method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM