Check if count of Alphabets and count of Numbers are equal in the given String | C ++ program to check if the count of alphabets and numbers in a string are equal or not . ; Function to count the number of alphabets ; Counter to store the number of alphabets in the string ; Every character in the string is iterated ; To check if the character is an alphabet or not ; Function to count the number of numbers ; Counter to store the number of alphabets in the string ; Every character in the string is iterated ; To check if the character is a digit or not ; Function to check if the count of alphabets is equal to the count of numbers or not ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countOfLetters ( string str ) { int letter = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) || ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) ) letter ++ ; } return letter ; } int countOfNumbers ( string str ) { int number = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] >= '0' && str [ i ] <= '9' ) number ++ ; } return number ; } void check ( string str ) { if ( countOfLetters ( str ) == countOfNumbers ( str ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; } int main ( ) { string str = " GeeKs01324" ; check ( str ) ; return 0 ; }