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

Given 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 alphabets are 3 whereas the count of numbers are 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 alphabets
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 numbers
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 not
void check(string str)
{
    if (countOfLetters(str)
        == countOfNumbers(str))
        cout << "Yes\n";
    else
        cout << "No\n";
}
  
// Driver code
int main()
{
    string str = "GeeKs01324";
    check(str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 alphabets
static 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 numbers
static 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 not
static void check(String str)
{
    if (countOfLetters(str)
        == countOfNumbers(str))
        System.out.print("Yes\n");
    else
        System.out.print("No\n");
}
  
// Driver code
public static void main(String[] args)
{
    String str = "GeeKs01324";
    check(str);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check if the count of 
# alphabets and numbers in a string 
# are equal or not. 
  
# Function to count the 
# number of alphabets 
def 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 numbers 
def 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 not 
def check(string) : 
  
    if (countOfLetters(string) == countOfNumbers(string)) :
        print("Yes"); 
    else :
        print("No"); 
  
# Driver code 
if __name__ == "__main__"
  
    string = "GeeKs01324"
    check(string); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 alphabets
static 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 numbers
static 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 not
static void check(String str)
{
    if (countOfLetters(str)
        == countOfNumbers(str))
        Console.Write("Yes\n");
    else
        Console.Write("No\n");
}
  
// Driver code
public static void Main(String[] args)
{
    String str = "GeeKs01324";
    check(str);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : 29AjayKumar, AnkitRai01

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.