📌  相关文章
📜  计算大写、小写、特殊字符和数值

📅  最后修改于: 2022-05-13 01:57:08.181000             🧑  作者: Mango

计算大写、小写、特殊字符和数值

给定一个字符串,编写一个程序来计算小写字符、大写字符、特殊字符和数字值的出现次数。
例子:

Input : #GeeKs01fOr@gEEks07
Output : 
Upper case letters : 5
Lower case letters : 8
Numbers : 4
Special Characters : 2

Input : *GeEkS4GeEkS*
Output :
Upper case letters : 6
Lower case letters : 4
Numbers : 1
Special Characters : 2

方法 :

  1. 将字符串str 从 0 扫描到 length-1。
  2. 根据 ASCII 值一次检查一个字符
    • if(str[i] >= 65 and str[i] <=90),则为大写字母,
    • if(str[i] >= 97 and str[i] <=122),则为小写字母,
    • if(str[i] >= 48 and str[i] <=57), 那么它是数字,
    • 否则它是一个特殊字符
  3. 打印所有计数器。

C++
// C++ program to count the uppercase,
// lowercase, special characters
// and numeric values
#include
using namespace std;
 
// Function to count uppercase, lowercase,
// special characters and numbers
void Count(string str)
{
    int upper = 0, lower = 0, number = 0, special = 0;
    for (int i = 0; i < str.length(); i++)
    {
        if (str[i] >= 'A' && str[i] <= 'Z')
            upper++;
        else if (str[i] >= 'a' && str[i] <= 'z')
            lower++;
        else if (str[i]>= '0' && str[i]<= '9')
            number++;
        else
            special++;
    }
    cout << "Upper case letters: " << upper << endl;
    cout << "Lower case letters : " << lower << endl;
    cout << "Number : " << number << endl;
    cout << "Special characters : " << special << endl;
}
 
// Driver function
int main()
{
    string str = "#GeeKs01fOr@gEEks07";
    Count(str);
    return 0;
}


Java
// Java program to count the uppercase,
// lowercase, special characters
// and numeric values
import java.io.*;
 
class Count
{
    public static void main(String args[])
    {
        String str = "#GeeKs01fOr@gEEks07";
        int upper = 0, lower = 0, number = 0, special = 0;
 
        for(int i = 0; i < str.length(); i++)
        {
            char ch = str.charAt(i);
            if (ch >= 'A' && ch <= 'Z')
                upper++;
            else if (ch >= 'a' && ch <= 'z')
                lower++;
            else if (ch >= '0' && ch <= '9')
                number++;
            else
                special++;
        }
 
        System.out.println("Lower case letters : " + lower);
        System.out.println("Upper case letters : " + upper);
        System.out.println("Number : " + number);
        System.out.println("Special characters : " + special);
    }
}


Python3
# Python 3 program to count the uppercase,
# lowercase, special characters
# and numeric values
 
# Function to count uppercase, lowercase,
# special characters and numbers
def Count(str):
    upper, lower, number, special = 0, 0, 0, 0
    for i in range(len(str)):
        if str[i].isupper():
            upper += 1
        else if str[i].islower():
            lower += 1
        else if str[i].isdigit():
            number += 1
        else:
            special += 1
    print('Upper case letters:', upper)
    print('Lower case letters:', lower)
    print('Number:', number)
    print('Special characters:', special)
 
# Driver Code
str = "#GeeKs01fOr@gEEks07"
Count(str)
 
# This code is contributed
# by Sushma Reddy


C#
// C# program to count the uppercase,
// lowercase, special characters
// and numeric values
using System;
 
class Count {
     
    public static void Main()
    {
         
        String str = "#GeeKs01fOr@gEEks07";
        int upper = 0, lower = 0;
        int number = 0, special = 0;
 
        for(int i = 0; i < str.Length; i++)
        {
            char ch = str[i];
            if (ch >= 'A' && ch <= 'Z')
                upper++;
            else if (ch >= 'a' && ch <= 'z')
                lower++;
            else if (ch >= '0' && ch <= '9')
                number++;
            else
                special++;
        }
        Console.WriteLine("Upper case letters : " + upper);
        Console.WriteLine("Lower case letters : " + lower);
        Console.WriteLine("Number : " + number);
        Console.Write("Special characters : " + special);
    }
}
 
// This code is contributed by Nitin Mittal.


PHP
= 'A' &&
            $str[$i] <= 'Z')
            $upper++;
        else if ($str[$i] >= 'a' &&
                 $str[$i] <= 'z')
            $lower++;
        else if ($str[$i]>= '0' &&
                 $str[$i]<= '9')
            $number++;
        else
            $special++;
    }
    echo "Upper case letters: " , $upper,"\n" ;
    echo "Lower case letters : " ,$lower,"\n" ;
    echo "Number : " , $number ,"\n";
    echo "Special characters : ", $special ;
}
 
    // Driver Code
    $str = "#GeeKs01fOr@gEEks07";
    Countt($str);
 
// This code is contributed by nitin mittal.
?>


Javascript


输出:

Upper case letters: 5
Lower case letters : 8
Number : 4
Special characters : 2

时间复杂度: O(N),其中 N 是字符串长度