My Code:
I tried the following code but the order of words are changing
var str = "Welcome to my Website !";
alert(str.split("").reverse().join(""));
It is giving result as
! etisbeW ym ot emocleW
But I need the result as
emocleW ot ym etisbeW !
That is, I don't want to change the order of words.
Simple Solution:Copy alphabetic characters from the given array to temp[]. Reverse temp[] using standard string reversal algorithm. Now traverse input string and temp in a single loop. Wherever there is an alphabetic character is input string, replace it with the current character of temp[].
In more simple language, the first word will be placed at the last position whereas the last word will come at the first place, the second word will be placed at the second last word's position and the second last word will come at the second place, and so on. In this way, our whole sentence is to be reversed.
Use this:
var str = "Welcome to my Website !";
alert(str.split("").reverse().join("").split(" ").reverse().join(" "));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With