Created
April 25, 2017 13:09
-
-
Save pawarvijay/7957b3d997767670e4c156e37722365b to your computer and use it in GitHub Desktop.
Reverse an array without affecting special characters from geeksforgeeks.com
This file contains hidden or 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
function isAlphabet(x) | |
{ | |
return ( (x >= 'A' && x <= 'Z') || | |
(x >= 'a' && x <= 'z') ); | |
} | |
function reverse(str) | |
{ | |
console.log('INPUT ARRAY ' + str.join('')) | |
var r = str.length - 1, l = 0; | |
while (l < r) | |
{ | |
if (!isAlphabet(str[l])) // Ignore special characters | |
{ | |
l++; | |
} | |
else if(!isAlphabet(str[r])) // Ignore special characters | |
{ | |
r--; | |
} | |
else // Both str[l] and str[r] are not spacial | |
{ | |
swap(l, r); | |
l++; | |
r--; | |
} | |
} | |
function swap(from , to){ | |
var temp = str[from]; | |
str[from] = str[to]; | |
str[to] = temp; | |
} | |
console.log('OUTPUT ARRAY ' + str.join('')) | |
} | |
var string = 'a!!!b.c.d,e ,f,ghi' | |
reverse(string.split('')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment