Permalink
Cannot retrieve contributors at this time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
""" | |
Write a `reverseWords` function that accepts a string a parameter, and reverses each word in the string. Every space should stay, so you cannot use `words` from `Prelude`. | |
Example: | |
``` | |
reverse_words["This is an example!"] # returns "sihT si na !elpmaxe" | |
``` | |
""" | |
def reverse_words[str]: | |
strList = [] | |
for word in str.split[' ']: | |
strList.append[word[::-1]] | |
return ' '.join[strList] |
Nội dung chính
- Reverse every word in a string
- How do you reverse words in an array?
- How do you reverse words in JavaScript?
- How do you reverse a string in an array?
- How do you reverse an array in JavaScript?
Reverse every word in a string
Photo by Lukas Robertson on UnsplashWe are going to write a function called reverseWords
that accepts a string, str
, as an argument.
You are given a string of varying word counts. The goal of the function is to reverse every word in the string and return it.
Example:
reverseWords['The quick brown fox jumps over the lazy dog.']
// output: 'ehT kciuq nworb xof spmuj revo eht yzal .god'
The first thing we are going to do is create a variable called reverseWordArr
. This variable will contain our string in array form.
let reverseWordArr = str.split[" "]
Since we want to create another array but containing all the words in the string reversed, we will use the map
method.
.map[word => word.split[""].reverse[].join[""]];
Here is the full variable:
let reverseWordArr = str.split[" "].map[word => word.split[""].reverse[].join[""]];
Now that we created our array and it contains all the words in the string but in reverse, we need to convert the array back to a string and return it.
return reverseWordArr.join[" "];
Here is the full function:
function reverseInPlace[str] {
var words = [];
words = str.split["\s+"];
var result = "";
for [var i = 0; i < words.length; i++] {
return result += words[i].split[''].reverse[].join[''];
}
}
console.log[reverseInPlace["abd fhe kdj"]]
What I expect is dba ehf jdk
, while what I'm getting here is jdk fhe dba
. What's the problem?
John Kugelman
336k66 gold badges509 silver badges559 bronze badges
asked Mar 19, 2018 at 10:26
5
This function should work for you:
function myFunction[string] {
return string.split[""].reverse[].join[""].split[" "].reverse[].join[" "]
};
allo
3,7546 gold badges36 silver badges66 bronze badges
answered Mar 19, 2018 at 10:42
you need to split the string by space
function reverseInPlace[str] {
var words = [];
words = str.match[/\S+/g];
var result = "";
for [var i = 0; i < words.length; i++] {
result += words[i].split[''].reverse[].join[''] + " ";
}
return result
}
console.log[reverseInPlace["abd fhe kdj"]]
answered Mar 19, 2018 at 10:31
AzadAzad
4,9863 gold badges27 silver badges53 bronze badges
3
Split
the string into words first before reversing the individual words
var input = "abd fhe kdj";
var output = input.split[ " " ].map[ //split into words and iterate via map
s => s.split[""].reverse[].join[ "" ] //split individual words into characters and then reverse the array of character and join it back
].join[ " " ]; //join the individual words
answered Mar 19, 2018 at 10:33
gurvinder372gurvinder372
65k9 gold badges69 silver badges90 bronze badges
const reverseWordIntoString = str => str.split[" "].map[word => word.split[""].reverse[].join['']].join[" "]
const longString = "My name is Vivekanand Panda";
const sentence = "I love to code";
const output = {
[longString]: reverseWordIntoString[longString],
[sentence]: reverseWordIntoString[sentence]
}
console.log[output];
answered Sep 8, 2021 at 10:39
You can make use of split
and map
function to create the reverse
words.
You need to first split the sentence
using space
and then you can just reverse each word and join the reversed words again.
function reverseWord [sentence] {
return sentence.split[' '].map[function[word] {
return word.split[''].reverse[].join[''];
}].join[' '];
}
console.log[reverseWord["abd fhe kdj"]];
answered Mar 19, 2018 at 10:36
If you are looking like this o/p : "Javascript is Best" => "Best is Javascript"
Then can be done by simple logic
//sample string
let str1 = "Javascript is Best";
//breaking into array
let str1WordArr = str1.split[" "];
//temp array to hold the reverse string
let reverseWord=[];
//can iterate the loop backward
for[let i=[str1WordArr.length]-1;i>=0;i--]
{
//pushing the reverse of words into new array
reverseWord.push[str1WordArr[i]];
}
//join the words array
console.log[reverseWord.join[" "]]; //Best is Javascript
answered May 2, 2020 at 10:06
This solution preserves the whitespaces characters space ␣
, the tab \t
, the new line \n
and the carriage return \r
] and preserves their order too [not reversed] :
const sentence = "abd\t fhe kdj";
function reverseWords[sentence] {
return sentence
.split[/[\s+]/]
.map[word => /^\s+$/.test[word] ? word : word.split[''].reverse[].join['']]
.join[''];
}
console.log[reverseWords[sentence]];
answered May 2, 2020 at 10:45
eroakeroak
1,00710 silver badges17 bronze badges
If you want to reverse a string by word:
Example: 'Welcome to JavaScript' to 'JavaScript to Welcome'
You can also do something like:
var str = 'Welcome to JavaScript'
function reverseByWord[s]{
return s.split[" "].reverse[].join[" "];
}
// Note: There is space in split[] and Join[] method
reverseByWord[str]
// output will be - JavaScript to Welcome
answered Feb 24, 2021 at 7:14
pradeepkspradeepks
531 silver badge6 bronze badges
You can easily achieve that by the following code
function reverseWords[str] {
// Go for it
let reversed;
let newArray=[];
reversed = str.split[" "];
for[var i = 0;iword.split[""].reverse[].join[""]].join[" "] return rev } console.log[reverse["soumya prakash"]];//o/p=>aymuos hsakarp
answered Sep 12 at 7:23
1
How do you reverse words in an array?
Explanation. Use split[''] , reverse[] and join[''] to convert to an array of characters, then reverse it and convert back to string. Use indexOf to find the index of the reverse
string in the array [ -1 if there are no matches].
How do you reverse words in JavaScript?
Reverse every word in a string.
let reverseWordArr = str.split[" "].
. map[word => word. split[""]. reverse[]. join[""]];.
let reverseWordArr = str. split[" "]. map[word => word. split[""]. reverse[]. join[""]];.
return reverseWordArr.join[" "];.
How do you reverse a string in an array?
Reverse a String using character array in Java.
Create an empty character array of the same size as that of the given string..
Fill the character array backward with characters of the given string..
Finally, convert the character array into string using String. copyValueOf[char[]] and return it..
How do you
reverse an array in JavaScript?
Description. Javascript array reverse[] method reverses the element of an array. ... .
Syntax. Its syntax is as follows − array.reverse[];.
Return Value. Returns the reversed single value of the array..
Example. Try the following example. ... .
Output. Reversed array is : 3,2,1,0..