📌  相关文章
📜  检查给定字符串中的字母数和数字数是否相等

📅  最后修改于: 2021-09-07 04:39:09             🧑  作者: Mango

给定字母数字字符串str ,任务是检查字母数和数字数是否相等。

例子:

方法:我们的想法是使用字符的ASCII值的号码和字母来区分。区分字符,维护两个计数器,分别统计字母和数字的个数。最后,检查两个计数器是否相等。
是字符的ASCII范围如下:

  • 小写字符:97 到 122
  • 大写字符:65 到 90
  • 数字:48 到 57

下面是上述方法的实现:

C++
// C++ program to check if the count of
// alphabets and numbers in a string
// are equal or not.
 
#include 
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


输出:
Yes

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live