字符串中的第一个字母增加,最后一个字母减少

时间:2018-02-03 04:24:36

标签: javascript string

我正在尝试创建一个简单的编码器,其中每个单词的第一个字母成为它的后继(A = B),最后一个字母成为它的前任(B = A)。

像这样:

KEY: The first letter goes up, the last letter goes backwards.
</p>

If it is the first letter in the string, <span style='color: red;'>x</span> should be converted to <span style='color: blue;'>y</span>, as seen below:
</p>
<span style='color: red;'>Aa</span> = <span style='color: blue;'>Bb</span>
</p>
<span style='color: red;'>Bb</span> = <span style='color: blue;'>Cc</span>
</p>
<span style='color: red;'>Cc</span> = <span style='color: blue;'>Dd</span>
</p>
<span style='color: red;'>Dd</span> = <span style='color: blue;'>Ee</span>
</p>
and so on....
</p>
If it is the last letter in the string, <span style='color: lightgreen;'>x</span> should be converted to <span style='color: orange;'>y</span>, as seen below:
</p>
<span style='color: lightgreen;'>Aa</span> = <span style='color: orange;'>Zz</span>
</p>
<span style='color: lightgreen;'>Bb</span> = <span style='color: orange;'>Aa</span>
</p>
<span style='color: lightgreen;'>Cc</span> = <span style='color: orange;'>Bb</span>
</p>
<span style='color: lightgreen;'>Dd</span> = <span style='color: orange;'>Cc</span>
</p>
and so on....
</p>

<div class="encode">
  The fox jumped over the dog. = Uhd gow kumpec pveq uhd eoh.
</div>

我正在尝试使用javaScript来实现这一点,但是,我愿意使用任何能够实现这一目标的语言。

这段代码是我正在寻找的要点,但我需要定位div中每个单词的第一个和最后一个字母:

var strRandomString = "I have 2 apples and 6 oranges and 3 grapes";
strRandomString.replace(/apples|oranges/g, function(m) {
    // `m` is a matched string.
    return m === 'apples' ? 'oranges' : 'apples';
})
// => "I have 2 oranges and 6 apples and 3 grapes"

不幸的是,我没有任何实际的示例javaScript代码(但上面的代码可以算作尝试,失败的代码),因为我已经研究了大约半小时但找不到可行的解决方案。

我已经搜索了以下主题寻求帮助:

https://codereview.stackexchange.com/questions/32620/replacing-every-letter-in-a-string-with-the-letter-following-it-in-the-alphabet

Promoting letters in a string to previous letter in java

https://coderanch.com/t/552363/java/Replacing-characters-string

http://www.dreamincode.net/forums/topic/326746-question-shifting-characters-to-the-next-letter-in-the-alphabet/

https://www.extendoffice.com/documents/excel/4027-excel-increase-letter-by-one.html(是的,我非常绝望,我将要使用excel)。

2 个答案:

答案 0 :(得分:2)

这适用于单词,但也适用于短语

function transform(phrase)
{ var i,last,letters,words=phrase.split(/\s+/);
  for(i=0;i<words.length;i++)
  { letters=words[i].split('');
    last=letters.length-1;
    letters[0]=letters[0].charCodeAt(0)<122?String.fromCharCode(letters[0].charCodeAt(0)+1):'a';
    letters[last]=97<letters[last].charCodeAt(0)?String.fromCharCode(letters[last].charCodeAt(0)-1):'z';
    words[i]=letters.join('');
  }
  return words.join(' ');
}

console.log(transform('apple zebra variables'));
//outputs 'bppld aebrz wariabler'

答案 1 :(得分:1)

为了说明这一点,我只是创建了一些函数然后调用它们。

这个问题,单字母词,如&#34; a&#34;得到双重混乱&#34; a&#34;成为&#34; z&#34;然后回到&#34; a&#34;作为第一个也是最后一个字母。

&#13;
&#13;
// I used this to remove the empty "" from the array, there might be better ways that altering the prototype but that might be opinion.
Array.prototype.clean = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};
// wrap to begin using same case
function prevLetter(letter) {
  if (letter === 'a') {
    return 'z';
  }
  if (letter === 'A') {
    return 'Z';
  }
  return String.fromCharCode(letter.charCodeAt(0) - 1);
}
// wrap to end using same case
function nextLetter(letter) {
  if (letter === 'z') {
    return 'a';
  }
  if (letter === 'Z') {
    return 'A';
  }
  return String.fromCharCode(letter.charCodeAt(0) + 1);
}
// last letter of word
function getLastLetter(myword) {
  return myword.slice(-1);
}
// first letter of word
function getFirstLetter(myword) {
  return myword.charAt(0);
}
//split sentence into array by the ;,. and space
function splitString(mystring) {
  return mystring.split(/[ ;,.]+/);
}
// replace a character (first,last)
function setCharAt(mystring, index, chr) {
  if (index > mystring.length - 1) return mystring;
  return mystring.substr(0, index) + chr + mystring.substr(index + 1);
}
var mySentence = "The big,  brown; cow ran over a zebra. I get this";
// words in the sentence to mess with
var splitS = splitString(mySentence).clean("");

for (var i = 0; i < splitS.length; i++) {
  var newS = setCharAt(splitS[i], 0, nextLetter(getFirstLetter(splitS[i])));
  var newSE = setCharAt(newS, newS.length - 1, prevLetter(getLastLetter(newS)));
  splitS[i] = newSE;
}
// the words in the array now are messed with
console.log(splitS);
&#13;
&#13;
&#13;