-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_wordReverse.js
67 lines (52 loc) · 1.25 KB
/
4_wordReverse.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"use strict";
let string = " Hi Hello How Are You";
// console.log(string.split(" ").reverse().join(" "));
let i;
if (typeof string != "string") {
console.log("...........WARNING!...........");
console.log("String value is only accepted!")
}
else {
WordReverse(string);
}
function WordReverse(string) {
let length = string.length;
//.................................
//word count
let word = 0;
for (i = 0; i <= length; i++) {
if (string[i] == " ") {
word = word + 1;
}
}
word = word + 1;
//.................................
let diff = 0;
let reverse = "";
let count = 0;
//.................................
for (let k = 1; k < word; k++) {
length = (length - count);
i = length;
count = 0;
while (string[i - 1] != " ") {
count = count + 1;
i--;
}
count = count + 1;
diff = (length - count);
let j = 0;
while (j < count) {
reverse += string[diff + j]
j++;
}
}
reverse = reverse + " ";
i = 0;
while (string[i] != " ") {
reverse += string[i];
i++;
}
//..............................
console.log(reverse);
}