PASCAL一道简单题
解决时间 2021-05-14 22:01
- 提问者网友:送舟行
- 2021-05-14 16:49
输入一串字符,以'?'结束,统计其中字母个数,数字个数,其他符号个数。
用while循环做
最佳答案
- 五星知识达人网友:荒野風
- 2021-05-14 17:12
PROGRAM count(input,output);
{输入字符,统计字母个数,数字个数}
VAR
ch:char;
num1,num2,num3:integer;
BEGIN
{将字母个数,数字个数,其它符号个数清零}
num1:=0;
num2:=0;
num3:=0;
{读入字符}
read(ch);
write(ch);
WHILE ch<>'?' DO
BEGIN
{判断ch是否为字母,数字,其它符号,相应个数加1}
IF(ch>='a')AND(ch<='z')
THEN num1:=num1+1
ELSE IF(ch>='0')AND(ch<='9')
THEN num2:=num2+1
ELSE num3:=num3+1;
read(ch);
write(ch)
END;
{输出统计结果}
writeln;
writeln('number of letter:',num1);
writeln('number of digit:',num2);
writeln('number of other:',num3)
END.
全部回答
- 1楼网友:一袍清酒付
- 2021-05-14 19:04
var
s:char;
a,b,c:integer;
begin
a:=0; b:=0; c:=0;
repeat
read(s);
if s in ['0'..'9'] then inc(a) else
if s in ['a'..'z']+['A'..'Z'] then inc(b) else
inc(c);
until s='?';
writeln('a=',a,' b=',b,' c',c);
end.
- 2楼网友:低血压的长颈鹿
- 2021-05-14 18:32
var
s:string;
i,a,b,c:integer;
begin
readln(s);
a:=0; b:=0; c:=0;
for i:=1 to length(s) do
if s[i] in ['0'..'9'] then inc(a) else
if s[i] in ['a'..'z']+['A'..'Z'] then inc(b) else
inc(c);
writeln('alpha:',a,#10,'digit:',b,#10,'other:',c);
end.
我要举报
大家都在看
推荐资讯