Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawarvijay/7957b3d997767670e4c156e37722365b to your computer and use it in GitHub Desktop.
Save pawarvijay/7957b3d997767670e4c156e37722365b to your computer and use it in GitHub Desktop.
Reverse an array without affecting special characters from geeksforgeeks.com
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