-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathreverse_words_in_a_string.js
36 lines (30 loc) · 1.7 KB
/
reverse_words_in_a_string.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/// Problem Statement:
/// You are given a string of length N. You need to reverse the string word by word. There can be multiple spaces between two words and there can be leading or trailing spaces but, in the output, reversed string you need to put a single space between two words, and your reversed string should not contain leading or trailing spaces.
/// Sample Input: You need to reverse the string word by word
/// Expected Output: word by word string the reverse to need You
/// Decalaration of function which will receive string to reverese
function reverseString(inputString) {
/// Trim function removes all the leading and trailing spaces, Split method store them in the array inputStringArray
var trimmedString = inputString.trim();
var inputStringArray = trimmedString.split(" ");
/// Variable declaration to store the output result
var outputString = "";
/// Loop through the array in reverse order to reverse the string, subtracting -1 from length because length starts from 1 but index strats from 0
for (i = inputStringArray.length - 1; i >= 0; i--) {
/// If its a first iteration then store the word in variable otherwise concatenate it with the current string
if (inputStringArray[i] == "") {
continue;
} else {
outputString += inputStringArray[i] + " ";
}
}
return outputString.trim();
}
// console.log("You are given a string of length N");
// console.log(reverseString("You are given a string of length N"));
console.log(" You need to reverse the string word by word ");
console.log(
reverseString(
" You need to reverse the string word by word "
)
);