0% found this document useful (0 votes)
2 views4 pages

String Q27

The document outlines four programming problems: removing vowels from a string, finding the longest word in a sentence, capitalizing the first letter of each word, and reversing letters in each word. Each problem includes a solution with example inputs and outputs, along with explanations of the code. Additionally, it promotes various PDF resources and courses for interview preparation and skill development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

String Q27

The document outlines four programming problems: removing vowels from a string, finding the longest word in a sentence, capitalizing the first letter of each word, and reversing letters in each word. Each problem includes a solution with example inputs and outputs, along with explanations of the code. Additionally, it promotes various PDF resources and courses for interview preparation and skill development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Remove Vowels from a String

1. Problem: Write a program to remove all vowels (a, e, i, o, u) from a given string.

Input:

"JavaScript is fun";

Output:

"JvScrpt s fn";

Solution:

function removeVowels(str) {
return str.replace(/[aeiouAEIOU]/g, ""); // Remove vowels using regex
}

// Test cases
console.log(removeVowels("JavaScript is fun")); // Output: "JvScrpt s fn"
console.log(removeVowels("Hello World")); // Output: "Hll Wrld"
console.log(removeVowels("I love programming")); // Output: " lv prgrmmng"
console.log(removeVowels("AEIOU are vowels")); // Output: " r vwls"

Explanation:

1. Use replace() with a regular expression (/[aeiouAEIOU]/g) to match all vowels.


2. Replace them with an empty string (""), effectively removing them.

Find the Longest Word in a Sentence

2. Problem: You are given a sentence as a string. Write a program to find the longest
word in the sentence.

Input:

"JavaScript makes coding enjoyable";

Output:

"JavaScript";

Solution:
function findLongestWord(sentence) {
let words = sentence.split(" ");
let longest = "";

for (let word of words) {


if (word.length > longest.length) {
longest = word;
}
}

return longest;
}

// Test cases
console.log(findLongestWord("JavaScript makes coding enjoyable")); // Output: "JavaScript"
console.log(findLongestWord("I love programming")); // Output: "programming"
console.log(findLongestWord("The quick brown fox jumps over the lazy dog")); // Output:
"jumps"
console.log(findLongestWord("Hello world!")); // Output: "Hello"

Explanation:

1. Split the string into an array of words.


2. Iterate through the words and compare their lengths.
3. Return the word with the maximum length.

Capitalize the First Letter of Each Word

3. Problem: Write a program to capitalize the first letter of each word in a given
sentence.

Input:

"javascript is powerful";

Output:

"Javascript Is Powerful";

Solution:

function capitalizeWords(sentence) {
return sentence
.split(" ") // Split the sentence into words
.map((word) => word.charAt(0).toUpperCase() + word.slice(1)) // Capitalize first letter
.join(" "); // Join words back into a sentence
}

// Test cases
console.log(capitalizeWords("javascript is powerful")); // Output: "Javascript Is Powerful"
console.log(capitalizeWords("hello world")); // Output: "Hello World"
console.log(capitalizeWords("i love coding")); // Output: "I Love Coding"
console.log(capitalizeWords("a quick brown fox")); // Output: "A Quick Brown Fox"

Explanation:

1. Split the string into words.


2. Capitalize the first letter using .charAt(0).toUpperCase() + word.slice(1).
3. Join the modified words back into a sentence.

Reverse Letters in Each Word

4. Problem: You are given a string with words separated by spaces. Write a program to
reverse the letters in each word while keeping the word order the same.

Examples:

Input:

"tpircSavaJ si emosewa"

Solution:
function reverseLettersInWords(sentence) {
return sentence
.split(" ") // Split sentence into words
.map((word) => word.split("").reverse().join("")) // Reverse each word
.join(" "); // Join words back into a sentence
}

// Test cases
console.log(reverseLettersInWords("JavaScript is awesome")); // Output: "tpircSavaJ si
emosewa"
console.log(reverseLettersInWords("I love coding")); // Output: "I evol gnidoc"
console.log(reverseLettersInWords("Hello World")); // Output: "olleH dlroW"
console.log(reverseLettersInWords("SingleWord")); // Output: "droWelgniS"

Explanation:

1. Split the string into an array of words.


2. Reverse the letters of each word using .split("").reverse().join("").
3. Join the modified words back into a string.
For More Interview Questions:
Looking to ace your next opportunity? Prepare with our expert-curated resources!

Get Your PDF Files Instantly:

Structured learning made simple! Choose from our affordable guides:

1. Frontend Development PDF = ₹99/-


Covers ReactJS, and JavaScript.
2. MERN Stack Development PDF = ₹149/-
Comprehensive guide for Frontend, Backend, and Database development.
3. Backend Development PDF = ₹59/-
Focused on Node.js, Express.js, MongoDB, and related technologies.

Coming Soon: 500+ Best Questions from LeetCode and HackerRank


Carefully selected questions frequently asked by top companies to help you ace your
interviews!

How to Get It?

• Message us on WhatsApp: 9398123134


• Pay the respective amount, and receive your PDF instantly!

Instant replies are available between 8:00 AM to 10:00 PM IST.

Join Our Online and Offline Classes

Upskill with confidence—our courses come with a


100% PLACEMENT GUARANTEE!

1. Frontend Technologies
Learn ReactJS, Angular, and modern UI/UX design.
2. Full Stack Development
Master MERN stack, Java, Python, or .NET technologies.
3. Data Science and Analytics
Gain skills in data visualization, modeling, and analysis.
4. Artificial Intelligence and Machine Learning
Explore AI tools, concepts, and practical implementations.
5. Advanced Topics
o Cloud Computing: AWS, Azure
o Cybersecurity: Ethical hacking, penetration testing

How to Enroll?

Call us at 9398123134 to get started today!

You might also like