首页 > 试题广场 >

统计字符

[编程题]统计字符
  • 热度指数:165484 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的由可见字符和空格组成的字符串,统计其中英文字母、空格、数字和其它字符的个数。

\hspace{15pt}字符串由 ASCII 码在 32126 范围内的字符组成。您可以参阅下表获得其详细信息。

../图片/可见字符集Ascii.png

输入描述:
\hspace{15pt}在一行上输入一个长度为 1 \leqq {\rm length}(s) \leqq 1000 的字符串。


输出描述:
\hspace{15pt}第一行输出一个整数,代表字符串中英文字母的个数。
\hspace{15pt}第二行输出一个整数,代表字符串中空格的个数。
\hspace{15pt}第三行输出一个整数,代表字符串中数字的个数。
\hspace{15pt}第四行输出一个整数,代表字符串中其它字符的个数。
示例1

输入

1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][

输出

26
3
10
12
def main():
    s = input()
    letters = []  # 存储英文字母
    spaces = []  # 存储空格
    digits = []  # 存储数字
    others = []  # 存储其他字符
    for char in s:
        if char.isalpha():
            letters.append(char)
        elif char.isspace():
            spaces.append(char)
        elif char.isdigit():
            digits.append(char)
        else:
            others.append(char)
    print(len(letters))  # 输出英文字母的个数
    print(len(spaces))  # 输出空格的个数
    print(len(digits))  # 输出数字的个数
    print(len(others))  # 输出其他字符的个数


if __name__ == "__main__":
    main()

发表于 2025-01-16 11:58:15 回复(0)
import re
s = input()

a = re.findall(r"[a-zA-Z]",s)  #所有英文字母
b = re.findall(r" ",s) #空格
c = re.findall(r"[0-9]",s) #数字
d = re.findall(r"[^a-zA-Z0-9 ]",s) #除英文字母,空格之外的其他字符

print(len(a))
print(len(b))
print(len(c))
print(len(d))
发表于 2024-12-26 13:53:58 回复(1)
aaa = input()
r = [0, 0, 0, 0]
for i in aaa:
    if i.isupper()&nbs***bsp;i.islower():
        r[0] += 1
        continue
    elif i == " ":
        r[1] += 1
        continue
    try:
        int(i)
        r[2] +=1
        continue
    except:
        pass
    r[3] += 1

for i in r:
    print(i)

发表于 2024-10-02 11:05:21 回复(0)
import re
s = input()
e = [0,0,0,0]
for l in s:
    if re.findall(r'[a-zA-Z]+', l):
        e[0] += 1
    elif l == ' ':
        e[1] += 1
    elif re.findall(r'[0-9]+', l):
        e[2] += 1
    else:
        e[3] += 1
# print(e, type(e))
print('\n'.join(map(str, e)))
# print(''.join(map(str, e)))


发表于 2024-06-02 19:07:29 回复(0)
"""

from numpy import s_
统计各个字符的数量,有两种方法
1. 正则匹配
2. 使用count
"""

s = input()

# 找出元
s_sole = list(set(list(s)))

letter_count = 0
space_count = 0
num_count = 0
other_count = 0

for letter in s:
    if letter.isalpha():
        letter_count += 1
    elif letter.isdigit():
        num_count += 1
    elif letter == ' ':
        space_count += 1
    else:
        other_count += 1

print(letter_count)
print(space_count)
print(num_count)
print(other_count)


发表于 2024-05-12 09:53:34 回复(0)
s=input()
zm,kg,sz,qt=0,0,0,0
for i in s:
    if i.isalnum():
        zm+=1
    elif i == ' ':
        kg+=1
    elif i.isdigit():
        sz+=1
    else:
        qt+=1
print(f"{zm}\n{kg}\n{sz}\n{qt}")

发表于 2024-05-11 16:42:02 回复(0)
s = input()
nalph, nspace, ndigit, nother = 0, 0, 0, 0

for i in s:
    if i.isalpha():
        nalph += 1
    elif i == ' ':
        nspace += 1
    elif i.isdigit():
        ndigit += 1
    else:
        nother += 1
print(nalph)
print(nspace)
print(ndigit)
print(nother)

发表于 2024-04-15 17:05:52 回复(0)
str=input()
num_alpha=0
num_digit=0
num_space=0
num_else=0
for i in str:
    if i.isalpha():
        num_alpha+=1
    elif i.isdigit():
        num_digit+=1
    elif i.isspace():
        num_space+=1
    else:
        num_else+=1
print(num_alpha)
print(num_space)
print(num_digit)
print(num_else)

发表于 2024-04-13 16:31:33 回复(0)
st = input()
c1 = 0 c2 = 0 c3 = 0 c4 = 0 for i in st: if i.isalpha():
        c1 += 1  elif i == ' ':
        c2 += 1  elif i.isnumeric():
        c3 += 1  else:
        c4 += 1 print(c1) print(c2) print(c3) print(c4)


发表于 2024-03-16 14:36:23 回复(0)
upper_alphabet = [chr(i) for i in range(ord('A'),ord('Z')+1)]
lower_alphabet = [chr(i) for i in range(ord('a'),ord('z')+1)]

numbers = [str(i) for i in range(10)]

input_str = input()
alphabet_num = 0
space_num = 0
number_num = 0
others_num = 0

for c in input_str:
    if c in (upper_alphabet + lower_alphabet):
        alphabet_num += 1
    elif c == ' ':
        space_num += 1
    elif c in numbers:
        number_num += 1
    else:
        others_num += 1
print(alphabet_num)
print(space_num)
print(number_num)
print(others_num)

发表于 2023-09-27 21:17:22 回复(0)
string = input()
count_alpha = 0
count_blank = 0
count_digit = 0
count_other = 0
for code in string:
    if code.isalpha():
        count_alpha += 1
    elif code == ' ':
        count_blank += 1
    elif code.isdigit():
        count_digit += 1
    else:
        count_other += 1
print(count_alpha)
print(count_blank)
print(count_digit)
print(count_other)

发表于 2023-09-17 16:29:14 回复(0)
正则表达式秒
import re
input_str = input()
print(len(re.findall('[a-zA-Z]',input_str)))
print(len(re.findall('\s',input_str)))
print(len(re.findall('[0-9]',input_str)))
print(len(re.findall('[^0-9a-zA-Z\s]',input_str)))


发表于 2023-07-21 16:04:03 回复(0)
strs = input()
count = [0] * 4
for c in strs:
    if c.isalpha(): count[0] += 1
    elif c.isspace(): count[1] += 1
    elif c.isdigit(): count[2] += 1
    else:
        count[3] += 1

print(*count, sep='\n')

发表于 2023-06-28 02:41:30 回复(0)
a1 = input()
a, b, c, d = 0, 0, 0, 0
for i in a1:
    if i.isalpha():
        a += 1
    elif i.isspace():
        b += 1
    elif i.isdigit():
        c += 1
    else:
        d += 1

print(a,b,c,d,sep="\n")

发表于 2023-06-10 18:18:24 回复(0)
import sys

def check_eng(c):
    if ord(c) >= ord('A') and ord(c) <= ord('Z'):
        return True
    if ord(c) >= ord('a') and ord(c) <= ord('z'):
        return True
    return False


for line in sys.stdin:
    x1 = 0
    x2 = 0
    x3 = 0
    x4 = 0

    line = line.rstrip()
    for c in line:
        if check_eng(c):
            x1 += 1
        elif c == " ":
            x2 += 1
        else:
            try:
                int(c)
                x3 += 1
            except:
                x4 += 1
    print(x1)
    print(x2)
    print(x3)
    print(x4)

发表于 2023-06-06 23:40:39 回复(0)
text=input()
e,s,n,o=0,0,0,0
for i in text:
    if i.isalpha():
        e+=1
    elif i==' ':
        s+=1
    elif i.isnumeric():
        n+=1
    else:
        o+=1
for i in [e,s,n,o]:
    print(i)

发表于 2023-04-20 11:23:27 回复(0)
import re


a = input()

if len(a) in range(1, 1001):
    cou_num = [0, 0, 0, 0]
    cou_num[0] = len(re.findall(r"[a-zA-Z]", a))
    cou_num[1] = len(re.findall(r"[\s]", a))
    cou_num[2] = len(re.findall(r"[\d]", a))
    cou_num[3] = len(a) - sum(cou_num[0:3])

    for i in cou_num:
        print(i)

发表于 2023-04-09 00:02:05 回复(0)
import re

str = input().strip()


rletter = re.findall(r'[a-z]|[A-Z]',str)
rnumber = re.findall(r'\d',str)
rblackspace = re.findall(r'\s',str)
rother = re.findall(r'[^\w\s]|_',str)



print(len(rletter))
print(len(rblackspace))
print(len(rnumber))
print(len(rother))

发表于 2023-04-07 15:44:29 回复(0)
str = input()
zy = 0
kg = 0
num =0
el = 0
for i in str:
    if i == ' ':
        kg += 1
    elif '0' <= i <= '9':
        num += 1
    elif 65 <= ord(i) <=90&nbs***bsp;97 <= ord(i) <= 122:
        zy += 1
    else: el += 1
print(f"{zy}\n{kg}\n{num}\n{el}\n")

发表于 2023-03-31 19:32:04 回复(0)
s = input()
if 1 <= len(s) <= 1000:
    flag = [0,0,0,0]
    for w in s:
        if w.isalpha():
            flag[0] += 1
        elif w == " ":
            flag[1] += 1
        elif w.isdigit():
            flag[2] += 1
        else:
            flag[3] += 1
    for i in flag:
        print(i)

发表于 2023-03-13 18:22:55 回复(0)