Dada la string alfanumérica str , la tarea es verificar si el conteo de Alfabetos y el conteo de Números son iguales o no.
Ejemplos:
Entrada: str = «GeeKs01234»
Salida: Sí
Explicación:
El recuento de letras y números es igual a 5.Entrada: str = “Gfg01234”
Salida: No
Explicación:
La cuenta del alfabeto es 3, mientras que la cuenta de los números es 5.
Enfoque: La idea es utilizar los valores ASCII de los caracteres para distinguir entre un número y un alfabeto. Después de distinguir los caracteres, se mantienen dos contadores para contar el número de letras y números respectivamente. Finalmente, se comprueba la igualdad de ambos contadores.
Los rangos ASCII de los caracteres son los siguientes:
- Caracteres en minúsculas: 97 a 122
- Caracteres en mayúsculas: 65 a 90
- Dígitos: 48 a 57
A continuación se muestra la implementación del enfoque anterior:
C++
// 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; }
Java
// 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
Python3
# 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
C#
// 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
Javascript
<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 alphabets function 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 numbers function 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 not function 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