Check if count of Alphabets and count of Numbers are equal in the given String

20 May, 2021
Given the alphanumeric string str, the task is to check if the count of Alphabets and count of Numbers are equal or not. 
Examples:  
Input: str = “GeeKs01234” Output: Yes Explanation: The count of alphabets and numbers are equal to 5. 
Input: str = “Gfg01234” Output: No Explanation: The count of the alphabet is 3, whereas the count of numbers is 5.  
Approach: The idea is to use the ASCII values of the characters to distinguish between a number and an alphabet. After distinguishing the characters, two counters are maintained to count the number of alphabets and numbers respectively. Finally, both the counters are checked for equality. The ASCII ranges of the characters are as follows:  
Lowercase characters: 97 to 122
Uppercase characters: 65 to 90
Digits: 48 to 57
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to check if the count of// alphabets and numbers in a string// are equal or not. #include <iostream>using namespace std; // Function to count the// number of alphabetsint countOfLetters(string str){    // Counter to store the number    // of alphabets in the string    int letter = 0;     // Every character in the string    // is iterated    for (int i = 0; i < str.length(); i++) {         // To check if the character is        // an alphabet or not        if ((str[i] >= 'A' && str[i] <= 'Z')            || (str[i] >= 'a' && str[i] <= 'z'))            letter++;    }     return letter;} // Function to count the number of numbersint countOfNumbers(string str){    // Counter to store the number    // of alphabets in the string    int number = 0;     // Every character in the string is iterated    for (int i = 0; i < str.length(); i++) {         // To check if the character is        // a digit or not        if (str[i] >= '0' && str[i] <= '9')            number++;    }     return number;} // Function to check if the// count of alphabets is equal to// the count of numbers or notvoid check(string str){    if (countOfLetters(str)        == countOfNumbers(str))        cout << "Yes\n";    else        cout << "No\n";} // Driver codeint main(){    string str = "GeeKs01324";    check(str);    return 0;}
// Java program to check if the count of// alphabets and numbers in a String// are equal or not.class GFG{ // Function to count the// number of alphabetsstatic int countOfLetters(String str){    // Counter to store the number    // of alphabets in the String    int letter = 0;     // Every character in the String    // is iterated    for (int i = 0; i < str.length(); i++)    {         // To check if the character is        // an alphabet or not        if ((str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')            || (str.charAt(i) >= 'a' && str.charAt(i) <= 'z'))            letter++;    }     return letter;} // Function to count the number of numbersstatic int countOfNumbers(String str){    // Counter to store the number    // of alphabets in the String    int number = 0;     // Every character in the String is iterated    for (int i = 0; i < str.length(); i++)    {         // To check if the character is        // a digit or not        if (str.charAt(i) >= '0' && str.charAt(i) <= '9')            number++;    }     return number;} // Function to check if the// count of alphabets is equal to// the count of numbers or notstatic void check(String str){    if (countOfLetters(str)        == countOfNumbers(str))        System.out.print("Yes\n");    else        System.out.print("No\n");} // Driver codepublic static void main(String[] args){    String str = "GeeKs01324";    check(str);}} // This code is contributed by 29AjayKumar
# Python3 program to check if the count of# alphabets and numbers in a string# are equal or not. # Function to count the# number of alphabetsdef countOfLetters(string ) :         # Counter to store the number    # of alphabets in the string    letter = 0;     # Every character in the string    # is iterated    for i in range(len(string)) :         # To check if the character is        # an alphabet or not        if ((string[i] >= 'A' and string[i] <= 'Z')            or (string[i] >= 'a' and string[i] <= 'z')) :            letter += 1;         return letter; # Function to count the number of numbersdef countOfNumbers(string ) :     # Counter to store the number    # of alphabets in the string    number = 0;     # Every character in the string is iterated    for i in range(len(string)) :         # To check if the character is        # a digit or not        if (string[i] >= '0' and string[i] <= '9') :            number += 1;     return number; # Function to check if the# count of alphabets is equal to# the count of numbers or notdef check(string) :     if (countOfLetters(string) == countOfNumbers(string)) :        print("Yes");    else :        print("No"); # Driver codeif __name__ == "__main__" :     string = "GeeKs01324";    check(string); # This code is contributed by AnkitRai01
// C# program to check if the count of// alphabets and numbers in a String// are equal or not.using System; class GFG{ // Function to count the// number of alphabetsstatic int countOfLetters(String str){    // Counter to store the number    // of alphabets in the String    int letter = 0;     // Every character in the String    // is iterated    for (int i = 0; i < str.Length; i++)    {         // To check if the character is        // an alphabet or not        if ((str[i] >= 'A' && str[i] <= 'Z')            || (str[i] >= 'a' && str[i] <= 'z'))            letter++;    }     return letter;} // Function to count the number of numbersstatic int countOfNumbers(String str){    // Counter to store the number    // of alphabets in the String    int number = 0;     // Every character in the String is iterated    for (int i = 0; i < str.Length; i++)    {         // To check if the character is        // a digit or not        if (str[i] >= '0' && str[i] <= '9')            number++;    }     return number;} // Function to check if the// count of alphabets is equal to// the count of numbers or notstatic void check(String str){    if (countOfLetters(str)        == countOfNumbers(str))        Console.Write("Yes\n");    else        Console.Write("No\n");} // Driver codepublic static void Main(String[] args){    String str = "GeeKs01324";    check(str);}} // This code is contributed by 29AjayKumar
<script>// Javascript program to check if the count of// alphabets and numbers in a string// are equal or not.  // Function to count the// number of alphabetsfunction countOfLetters( str){    // Counter to store the number    // of alphabets in the string    var letter = 0;     // Every character in the string    // is iterated    for (var i = 0; i < str.length; i++) {         // To check if the character is        // an alphabet or not        if ((str[i] >= 'A' && str[i] <= 'Z')            || (str[i] >= 'a' && str[i] <= 'z'))            letter++;    }     return letter;} // Function to count the number of numbersfunction countOfNumbers( str){    // Counter to store the number    // of alphabets in the string    var number = 0;     // Every character in the string is iterated    for (var i = 0; i < str.length; i++) {         // To check if the character is        // a digit or not        if (str[i] >= '0' && str[i] <= '9')            number++;    }     return number;} // Function to check if the// count of alphabets is equal to// the count of numbers or notfunction check( str){    if (countOfLetters(str) == countOfNumbers(str))       document.write( "Yes<br>");    else       document.write( "No<br>");}  var str = "GeeKs01324";check(str); // This code is contributed by SoumikMondal</script>
Yes
 
29AjayKumar
ankthon
SoumikMondal
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Python Exception Handling
Python Try Except
Ternary Operator in Python
Write a program to reverse an array or string
Write a program to print all permutations of a given string
Check for Balanced Brackets in an expression (well-formedness) using Stack
Longest Common Subsequence | DP-4
KMP Algorithm for Pattern Searching