🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Vowels are a fundamental part of understanding and analyzing language. Counting the number of vowels in a string can be helpful in various text-processing tasks. In this post, we'll create a simple TypeScript function that counts the number of vowels in a given string.
2. Program Overview
We will define a function named countVowels that takes a string as an argument. This function will then return the number of vowels present in that string. For simplicity, we'll only consider the five primary English vowels: A, E, I, O, and U.
3. Code Program
// Function to count the number of vowels in a string
function countVowels(s: string): number {
// Convert the string to lowercase to make our search case-insensitive
const lowerCaseStr = s.toLowerCase();
// Define our vowels
const vowels = ['a', 'e', 'i', 'o', 'u'];
// Count and return the number of vowels in the string
return [...lowerCaseStr].filter(char => vowels.includes(char)).length;
}
// Test the function
const testStr = "Hello, World!";
console.log(`Number of vowels in "${testStr}" is:`, countVowels(testStr));
Output:
Number of vowels in "Hello, World!" is: 3
4. Step By Step Explanation
1. We start by defining our function countVowels which takes a string s as its argument.
2. To make our search case-insensitive, we convert the string to lowercase using the toLowerCase() method.
3. We define an array named vowels that contains all the vowels we want to search for.
4. To count the vowels, we spread our string into an array of characters using the spread syntax (...). We then filter this array, keeping only the characters that are in our vowels array using the filter() method combined with the includes() method.
5. The length of the resulting filtered array gives us the number of vowels in the original string, which we return.
6. Finally, we test our function with a sample string "Hello, World!" and print the result.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment