Day 76: Number of Characters, Vowels, Digits, Special Characters of 100DaysCodingChallenge
Hello all,
#day76
Day76
Today's my task was to Write a Program to Find the Number of Vowels, Consonants, Digits and Special Characters from a String using string input taken by the user.
I have started the 100 days of coding challenge to improve my programming skills..
Code :
// Program to count vowels, consonant, // digits and // special character in a given string. // Function to count number of vowels, consonant, // digitsand special character in a string. function countCharacterType(str) { // Declare the variable vowels, // consonant, digit // and special characters var vowels = 0, consonant = 0, specialChar = 0, digit = 0; // str.length() function to count number of // character in given string. for (var i = 0; i < str.length; i++) { var ch = str[i]; if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z")) { // To handle upper case letters ch = ch.toLowerCase(); if (ch == "a" || ch == "e" || ch == "i" || ch == "o" || ch == "u") vowels++; else consonant++; } else if (ch >= "0" && ch <= "9") digit++; else specialChar++; } console.log("Vowels: " + vowels + " "); console.log("Consonant: " + consonant + " "); console.log("Digit: " + digit + " "); console.log("Special Character: " + specialChar + " "); } // Driver function. //var str = "geeks for geeks121"; var str = prompt("Enter a String : "); countCharacterType(str);
Output :
Happy Coding:)
Thank you...
Comments
Post a Comment