JavaScript Program to Count Number of Alphabets
Last Updated :
16 Jul, 2024
We are going to implement an algorithm by which we can count the number of alphabets present in a given string. A string can consist of numbers, special characters, or alphabets.
Examples:
Input: Str = "adjfjh23"
Output: 6
Explanation: Only the last 2 are not alphabets.
Input: Str = "n0ji#k$"
Output: 4
Explanation: 0, #, and $ are not alphabets.
In this article, we are going to implement it with two different approaches.
Approach 1: Using RegExp
In this approach, we will use Regular Expression to count the number of alphabets in a string. A regular expression is a sequence of characters that forms a search pattern. The a-zA-Z pattern finds any character between the brackets a to z and A to Z.
Syntax:
/pattern/modifiers;
Example:
JavaScript
let str = "Geeks123for#$Geeks";
let regex = /[a-zA-Z]/g;
console.log(str.match(regex).length);
Approach 2: Using for Loop
In this approach, we will use for loop to traverse each character of a string, and use str.charAt() method to check the character value between 'A' to 'Z'.
Syntax:
for (statement 1 ; statement 2 ; statement 3){
code here...
}
Example:
JavaScript
let str = "geeks12354forgeeks";
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str.charAt(i) >= 'A' || str.charAt(i) >= 'Z') {
count++;
}
}
console.log(count);
Approach 3: Using String.prototype.split() and Array.prototype.filter()
This approach splits the string into an array of characters using `split()`, then filters out non-alphabetic characters using `filter()` with a regular expression, and returns the length of the filtered array.
Example:
JavaScript
function countAlphabets(str) {
return str.split('').filter(char => /[a-zA-Z]/.test(char)).length;
}
console.log(countAlphabets("Hello World")); // Output: 10
Approach 4: Using Array.prototype.reduce()
In this approach, we convert the string into an array of characters using split('') and then use the reduce() method to count how many of these characters are alphabets. The reduce() method will iterate through each character and maintain a count of those that match the alphabet pattern [a-zA-Z].
Example:
JavaScript
function countAlphabets(str) {
return str.split('').reduce((count, char) => {
return count + (/[a-zA-Z]/.test(char) ? 1 : 0);
}, 0);
}
console.log(countAlphabets("Hello World123!")); // Output: 10
Approach 5: Using Array.prototype.every()
In this approach, we first split the string into an array of characters using split(). We then use a for loop to iterate through each character, using the every() method to check if the character is an alphabet. We maintain a count of how many characters are alphabets.
Example:
JavaScript
function countAlphabets(str) {
let count = 0;
const characters = str.split('');
for (let i = 0; i < characters.length; i++) {
if (/[a-zA-Z]/.test(characters[i])) {
count++;
}
}
return count;
}
console.log(countAlphabets("Hello World123!")); // Output: 10
Approach 6: Using Array.prototype.map() and Array.prototype.reduce()
In this approach, we will first split the string into an array of characters using split(''). We then map each character to 1 if it is an alphabet (using a regular expression) or 0 otherwise. Finally, we use the reduce() method to sum up the values, giving us the count of alphabetic characters.
Example:
JavaScript
function countAlphabets(str) {
return str.split('')
.map(char => /[a-zA-Z]/.test(char) ? 1 : 0)
.reduce((sum, val) => sum + val, 0);
}
console.log(countAlphabets("Hello World123!"));
Similar Reads
JavaScript Program to Count the Occurrences of Each Character Here are the various methods to count the occurrences of each characterUsing JavaScript ObjectThis is the most simple and widely used approach. A plain JavaScript object (obj) stores characters as keys and their occurrences as values.JavaScriptconst count = (s) => { const obj = {}; for (const cha
3 min read
JavaScript Program to Count the Occurrences of a Specific Character in a String In this article, we will see how to count the frequency of a specific character in a string with JavaScript. Counting the frequency of a specific character in a string is a common task in JavaScript. Example: Input : S = âgeeksforgeeksâ and c = âeâOutput : 4Explanation: âeâ appears four times in str
3 min read
JavaScript - Frequency of characters in Alphabatic Order The operation involves counting how many times each character in alphabetic characters in a given string.Using Object (Simple and Efficient for Small to Moderate Strings)Object in JavaScript allow storing key value pairs. So we use items as keys and their frequencies as values.JavaScriptfunction pri
2 min read
Program to count the number of characters in a word Write a program to count the number of characters in a word. Examples: Input: Word: "programming"Output: 11 Input: Word: "hello"Output: 5 Approach: To solve the problem, follow the below idea: To count the number of characters in a word, maintain a variable to count all the characters. Iterate throu
2 min read
JavaScript - How to Get a Number of Vowels in a String? Here are the various methods to get the number of vowels in a string using JavaScript.1. Using a for LoopThis is the most basic and beginner-friendly approach. It uses a loop to iterate over each character and checks if it is a vowel.JavaScriptconst cVowels = (s) => { const vowels = "aeiouAEIOU";
3 min read
Java Program to Count Number of Vowels in a String In java, the string is a sequence of characters and char is a single digit used to store variables. The char uses 2 bytes in java. In java, BufferedReader and InputStreamReader are used to read the input given by the user from the keyboard. Then readLine() is used for reading a line. The java.io pac
4 min read
Java Program to Count Number of Digits in a String The string is a sequence of characters. In java, objects of String are immutable. Immutable means that once an object is created, it's content can't change. Complete traversal in the string is required to find the total number of digits in a string. Examples: Input : string = "GeeksforGeeks password
2 min read
Program to count the number of consonants in a word Write a program to count the number of consonants in a given word. Consonants are the letters of the English alphabet excluding vowels ('a', 'e', 'i', 'o', 'u'). Examples: Input: "programming"Output: 8 Explanation: The eight consonants are: 'p', 'r', 'g', 'r', 'm', 'm', 'n', 'g') Input: "hello"Outpu
5 min read
JavaScript program to print Alphabets from A to Z using Loop Our task is to print the alphabet from A to Z using loops. In this article, we will mainly focus on the following programs and their logic. Below are the loops used to print Alphabets from A to Z: Table of Content Using for loopUsing the while loopUsing a do-while loop Using for loopThe elements are
4 min read
Program to count the number of vowels in a word Write a program to count the number of vowels in a given word. Vowels include 'a', 'e', 'i', 'o', and 'u' (both uppercase and lowercase). The program should output the count of vowels in the word. Examples: Input: "programming"Output: 3Explanation: The word "programming" has three vowels ('o', 'a',
3 min read