Reverse the Order of Space-Separated Words in a String

Reverse the order of space-separated words in a string

Try this:

$s = 'Hello everybody in stackoverflow';
echo implode(' ', array_reverse(explode(' ', $s)));

Reverse the order of space separated words in a string

OrderBy sorts the array alphabetically, you need Reverse.

This code example returns it back into a string reversed..

var line = "5 Damaged batten type fluorescent Luminaire sited adjacent to the Cooler in the Beer Celler C2 No";
string[] lineTexts = line.Split(' ').Reverse().ToArray();
Console.Write(string.Join(" ",lineTexts));

Javascript How to reverse space separated strings

This should give you the result you're looking for.

function reverseWords(s) {
return s.replace(/[a-z]+/ig, function(w){return w.split('').reverse().join('')});
}

Reverse the ordering of words in a string

Reverse the entire string, then reverse the letters of each individual word.

After the first pass the string will be

s1 = "Z Y X si eman yM"

and after the second pass it will be

s1 = "Z Y X is name My"

How to reverse the order of the words in a string

You can use split to get the separate words, reverse to reverse the list, and finally join to join them again to make the final string:

s = "This is New York"
# split first
a = s.split()
# reverse list
a.reverse()
# now join them
result = " ".join(a)
# print it
print(result)

results in:

'York New is This'

How to reverse the words of a string considering the punctuation?

Try this (explanation in comments of code):

s = "Do or do not, there is no try."

o = []
for w in s.split(" "):
puncts = [".", ",", "!"] # change according to needs
for c in puncts:
# if a punctuation mark is in the word, take the punctuation and add it to the rest of the word, in the beginning
if c in w:
w = c + w[:-1] # w[:-1] gets everthing before the last char

o.append(w)


o = reversed(o) # reversing list to reverse sentence
print(" ".join(o)) # printing it as sentence

#output: .try no is there ,not do or Do

i stuck in leetcode problem 151. Reverse Words in a String

You have only one small typo in your code.

The line

        string word = s.substr(i,j-1);

should be

std::string word = s.substr(i, j - i);

So you mixed up i with 1.

No big deal.

#include <string>
#include <iostream>

std::string reverseWords(std::string s) {
std::string ans;
int i = 0;
int n = s.size();
while (i < n)
{
while (i < n and s[i] == ' ')
i++;
if (i >= n)
break;
int j = i + 1;
while (j < n and s[j] != ' ')
j++;
std::string word = s.substr(i, j - i);
if (ans.size() == 0)
ans = word;
else
ans = word + " " + ans;
i = j + 1;

}
return ans;
}
int main() {
std::cout << reverseWords("ab cd ef");
}

Need help to reverse a string of words

Using your code specifically, you are very close with what you are trying to do. You just need to move the declaration of the reversedWord variable out of the forEach loop, update inside the forEach loop, which will eventually be defined with the entire reversed word.

function reverseWords(string) {

var wordArray = string.split(" ");
var resultWordArray = [];
var requiredSentance;

wordArray.forEach(word => {

if (word == " ") {
var space = " ";
resultWordArray.push(space);

} else {
var SplittedWord = word.split("");
var reversedWordsLettersArray = [];
var reversedWord; //define variable here

SplittedWord.forEach(letter => {
reversedWordsLettersArray.unshift(letter);
reversedWord = reversedWordsLettersArray.join("");
})
resultWordArray.push(reversedWord); // push it here
}
})

var resultSentance = resultWordArray.join(" ");
console.log(resultSentance);
}

reverseWords("Reverse this line");

How to reverse words in a string instead of reversing the whole string?

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


Related Topics



Leave a reply



Submit