Reversal

by Saurabh Khemka

HTML

Reversing string without affecting any special characters

JavaScript

function reversal(str) {

  let l = 0,
    split = str.split(''),
    r = split.length - 1,
    temp;

  while (l < r) {
    // Ignore special characters 
    if (!isAlphabet(split[l]))
      l++;
    else if (!isAlphabet(split[r]))
      r--;

    // Both str[l] and str[r] are not spacial 
    else {
      var tmp = split[l];
      split[l] = split[r];
      split[r] = tmp;
      l++;
      r--;
    }
  }

	console.log(split)
  return split.join('');
}

function isAlphabet(char) {
  if (char.charCodeAt(0) >= 97 && char.charCodeAt(0) <= 122)
    return true;
  return false;
}

console.log(reversal('q$w#e:r'))