
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Reversing alphabets in a string using JavaScript
Problem
We are required to write a JavaScript function that takes in a string, str, which consists of alphabets and some special characters.
Our function should return a new string based on the input string where all characters that are not alphabets stay in the same place, and all letters reverse their positions.
For example, if the input to the function is
Input
const str = 'k_lmn_opq';
Output
const output = 'q_pon_mlk';
Example
const str = 'k_lmn_opq'; const reverseAlphabets = (str) => { const arr = str.split('') let left = 0 let right = arr.length - 1 const swap = (a, b) => { const temp = arr[a] arr[a] = arr[b] arr[b] = temp } const isLetter = (x = '') => /[a-zA-Z]/.test(x) while (left <= right) { while (!isLetter(arr[left])) { left += 1 if (left > right) { break } } while (!isLetter(arr[right])) { right -= 1 if (left > right) { break } } if (left > right) { break } swap(left, right) left += 1 right -= 1 } return arr.join('') }; console.log(reverseAlphabets(str));
Output
q_pon_mlk
- Related Articles
- Reversing a string using for loop in JavaScript
- Reversing vowels in a string JavaScript
- Reversing words in a string in JavaScript
- Reversing words within a string JavaScript
- Sorting alphabets within a string in JavaScript
- Reversing words present in a string in JavaScript
- Reversing consonants only from a string in JavaScript
- Converting a string to NATO phonetic alphabets in JavaScript
- Reversing the even length words of a string in JavaScript
- Reversing the order of words of a string in JavaScript
- Reversing a string while maintaining the position of spaces in JavaScript
- Check if a string contains only alphabets in Java using Regex
- Padding a string with random lowercase alphabets to fill length in JavaScript
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using ASCII values

Advertisements