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!"));