
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reverse word starting with particular characters - JavaScript
We are required to write a JavaScript function that takes in a sentence string and a character and the function should reverse all the words in the string starting with that particular character.
For example: If the string is −
const str = 'hello world, how are you';
Starting with a particular character ‘h’ −
Then the output string should be −
const output = 'olleh world, woh are you';
That means, we have reversed the words starting with “h” i.e. Hello and How.
Example
Following is the code −
const str = 'hello world, how are you'; const reverseStartingWith = (str, char) => { const strArr = str.split(' '); return strArr.reduce((acc, val) => { if(val[0] !== char){ acc.push(val); return acc; }; acc.push(val.split('').reverse().join('')); return acc; }, []).join(' '); }; console.log(reverseStartingWith(str, 'h'));
Output
Following is the output in the console −
olleh world, woh are you
- Related Questions & Answers
- Finding word starting with specific letter in JavaScript
- C# program to remove characters starting at a particular index in StringBuilder
- How to remove non-word characters in JavaScript?
- Program to replace all the characters in of a file with '#' except a particular word in Java
- Replacing zero starting with whitespace in JavaScript
- Group strings starting with similar number in JavaScript
- Reverse array with for loops JavaScript
- How to reverse a given string word by word instead of letters using C#?
- Python program to reverse each word in a sentence?
- Java program to reverse each word in a sentence
- JavaScript Array reverse()
- How to match word characters using Java RegEx?
- Query MongoDB collection starting with _?
- Write a java program to reverse each word in string?
- Array reverse() in JavaScript
Advertisements