首页 > 试题广场 >

统计字符

[编程题]统计字符
  • 热度指数: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
s = input()
a,b,c,d = (0,0,0,0)
for i in s:
    if i.isalpha() :
        a += 1
    elif i ==' ':
        b += 1
    elif i.isdigit():
        c += 1
    else:
        d += 1
print(a,b,c,d,sep='\n')

发表于 2024-06-24 18:18:27 回复(0)
S=input()
eng,mspace,dig,other=0,0,0,0
for i in S:
    if i.isdigit():
        dig+=1
    elif i.isalpha():
        eng+=1
    elif i.isspace():
        mspace+=1
    else:
        other+=1
print(eng,mspace,dig,other,sep="\n")
编辑于 2023-12-10 19:14:27 回复(0)
# 解法,使用ascall判断英文字母,注意小写字母的a是97-122,大写字母是65-90;所以在90到97之间是特殊符号,注意区分
#
def static(str1):
    try:
        j=0  # 空格
        k=0  # 数字
        z=0  # 字母
        w=0  # 特殊符号
        for i in str1:
            # 先统计空格
            if i == " ":
                j+=1
            # 统计数字
            elif ord(i) in range(ord("0"),ord("9")+1)  :
                k+=1
            # 判断是否为英文字符
            elif ord(i) in range(ord("A"),ord("Z")+1):
                z+=1
                # print(i)
            elif ord(i) in range(ord("a"),ord("z")+1):
                z += 1
                # print(i)
            else:
                w+=1
        
        print(z,j,k,w,sep='\n')
    except Exception as e:
        print(e)

if __name__ == "__main__":
    str1 = input()
    static(str1)


发表于 2022-08-28 00:19:39 回复(0)
s=input()
x='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
number='0123456789'
char,num,kong,qi=0,0,0,0
for i in s:
    if i in x:
        char+=1
    elif i in number:
        num+=1
    elif i==' ':
        kong+=1
    else:
        qi+=1
print(char,kong,num,qi,sep='\n')
发表于 2022-08-27 22:11:59 回复(0)
# 遍历一遍每个都进行判断就好了
s = input()
a, b, c, d = 0, 0, 0, 0
for i in s:
    if i.isalpha():
        a += 1
    elif i == " ":
        b += 1
    elif i.isdigit():
        c += 1
    else:
        d += 1
print(a)
print(b)
print(c)
print(d)
发表于 2022-08-25 14:47:33 回复(0)

import sys 
for line in sys.stdin:
    a = line    
result = {}
for i in a:
    if(i.isalpha()):
        if("alpha" not in result.keys()):
            result["alpha"] = 1
        else:
            result["alpha"] = result["alpha"]+1
    elif(i.isdigit()):
        if("digit" not in result.keys()):
            result["digit"] = 1
        else:
            result["digit"] = result["digit"]+1
    elif(i == " "):
        if("blank" not in result.keys()):
            result["blank"] = 1
        else:
            result["blank"] = result["blank"]+1
    else:
        if("other" not in result.keys()):
            result["other"] = 1
        else:
            result["other"] = result["other"]+1
if("alpha" in result.keys()):        
    print(result["alpha"])
else:
    print(0)
if("blank" in result.keys()):
    print(result["blank"])
else:
    print(0)
if("digit" in result.keys()):    
    print(result["digit"])
else:
    print(0)
if("other" in result.keys()):    
    print(result["other"]-1)
else:
    print(0)
我这个代码最后没办法必须要将other强行减一,我发现other总会多一个。是不是因为字符串里面最后还包含一个换行符?

发表于 2022-08-18 21:45:43 回复(0)
s = input()
#英文字符:
def alpha(s):
    for i in s:
        if not i.isalpha():
            s = s.replace(i,'')
    lenalpha = len(s)
    return lenalpha
print(alpha(s))
#空格数:
lenwhole = len(s)
lenspace = lenwhole-len(s.replace(' ',''))
print(lenspace)
#数字字符:
def digit(s):
    for i in s:
        if not i.isdigit():
            s = s.replace(i,'')
    lendigit = len(s)
    return lendigit
print(digit(s))
#其他字符数:
def other(s):
    for i in s:
        if i.isdigit()&nbs***bsp;i.isalpha():
            s = s.replace(i,'')
            s1 = s.replace(' ','')
    lenother = len(s1)
    return lenother
print(other(s))

发表于 2022-08-14 12:55:00 回复(0)
s = input()
ls = [0]*4
for i in s:
    if i.lower().islower():
        ls[0] += 1
    elif i.isspace():
        ls[1] += 1
    elif i.isdigit():
        ls[2] += 1
    else:
        ls[-1] += 1
for a in ls:
    print(a)

发表于 2022-08-03 17:39:20 回复(0)
nums = '0123456789'
abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
s = input()
a = 0
b = 0
c = 0
d = 0
for i in range(len(s)):
    if s[i] in abc:
        a =a+1
    elif s[i] == ' ':
        b = b+1
    elif s[i] in nums:
        c = c+1
    else:
        d = d +1
        
print(a)
print(b)
print(c)
print(d)


发表于 2022-08-01 22:27:33 回复(0)
ip = input()
spaceNum = ip.count(' ')
ip = ip.replace(' ','')

charNum = 0
numNum = 0
otherNum = 0

for char in ip:
    if ord('a')<=ord(char)<=ord('z')&nbs***bsp;ord('A')<=ord(char)<=ord('Z'):
        charNum += 1
    elif char in "0123456789":
        numNum += 1
    else:
        otherNum += 1                

print(charNum)
print(spaceNum)
print(numNum)
print(otherNum)

发表于 2022-07-28 21:55:09 回复(0)
to be python;

s = input()
res = [0, 0, 0, 0]
for i in s:
    if i.isalpha():
        res[0] += 1
    elif i == ' ':
        res[1] +=1
    elif i.isdigit():
        res[2] += 1
    else:
        res[3] += 1

for n in res:
    print(n)


发表于 2022-07-27 00:34:58 回复(0)
s = input()
hashtable = {'alpha': 0, ' ':0, 'dig':0, 'other':0}
for i in s:
    if i.isdigit():
        hashtable['dig'] += 1
    elif i.isalpha():
        hashtable['alpha'] += 1
    elif i == ' ':
        hashtable[' '] += 1
    else:
        hashtable['other'] +=1
for i in hashtable.values():
    print(i)

发表于 2022-07-25 19:14:12 回复(0)
python3 from collections import Counter, x.isalpha() / x.isdigit() / x.isspace() 
from collections import Counter

while True:
    try:
        words = input()
        words = Counter(words)
        count_alpha, count_space, count_digit, count_other = 0,0,0,0
        for word in words:
            if word.isalpha():
                count_alpha += words[word]
            elif word.isspace():
                count_space += words[word]
            elif word.isdigit():
                count_digit += words[word]
            else:
                count_other += words[word]
        print(count_alpha)
        print(count_space)
        print(count_digit)
        print(count_other)
    except:
        break


发表于 2022-07-16 16:59:57 回复(0)
a = input()
count = [0,0,0,0]
apl = 'qwertyuiopasdfghjklzxcvbnm'
num = '0123456789'
for i in a:
    if i in apl:
        count[0] += 1
    elif i == ' ':
        count[1] += 1        
    elif i in num:
        count[2] += 1
    else:
        count[3] += 1
for i in count:
    print(i)
    

发表于 2022-07-12 22:19:24 回复(0)
import re
a1 = input()
y = re.findall(r'[a-zA-Z]', a1)
k = re.findall(r' ', a1)
num = re.findall(r'[0-9]', a1)
q = len(a1) - len(y) -len(k)-len(num)
print(len(y))
print(len(k))
print(len(num))
print((q))
发表于 2022-07-02 09:50:13 回复(0)
inp = input().upper()

word_list = [chr(i) for i in range(ord('A'), ord('Z')+1)]

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

word, num = 0, 0

space = inp.count(' ')

for i in inp:
    
    if i in word_list:
        
        word += 1
        
    elif i in num_list:
        
        num += 1
        
    else:
        
        continue
        
print(str(word) + '\n' + str(space) + '\n' + str(num) + '\n' + str(len(inp)-word-num-space))

发表于 2022-07-01 03:07:51 回复(0)
n =input()
a,b,c,d = 0,0,0,0
for i in n:
    if i.isalpha():
        a +=1
    elif i.isspace():
        b +=1
    elif i.isalnum():
        c +=1
    else:
        d +=1
print(a)
print(b)
print(c)
print(d)
考察类型判断
发表于 2022-06-28 11:14:24 回复(0)
# Date:20220623
# Subject:统计字符
# a-z:97-122
# A-Z:65-90
# 0-9:48-57
# 空格:32

string = input()
arr = [0,0,0,0]
for i in string:
    if 65 <= ord(i) <= 90 or 97 <= ord(i) <= 122:
        arr[0] += 1
    elif 48 <= ord(i) <= 57:
        arr[2] += 1
    elif ord(i) == 32:
        arr[1] += 1
    else:
        arr[3] += 1
for i in range(4):
    print(arr[i])
发表于 2022-06-23 12:57:56 回复(0)