用VB 输入一行字符,统计出数字,字母,空格和其它字符的个数
展开全部
Option Explicit
Dim letters As Integer '声明模块级变量,此变量计算字母个数
Dim space As Integer '空格个数
Dim digit As Integer '数字个数
Dim others As Integer '其他字符个数
Private Sub Command1_Click()
Dim s As String
s = InputBox("请输入字符串")
Call jisuan(s)
'以下代码用来显示统计出的结果值
Print "字符串【" & s & "】包含:"
Print "英文字母数量="; letters
Print "空格数量="; space
Print "数字数量="; digit
Print "其他字符数量="; others
End Sub
Private Sub jisuan(InputStr As String) 'InputStr变量存储输入的字符串
Dim i As Integer '循环控制变量,整型
Dim CaseStr As String '此变量保存储所截取的字符
letters = 0 '初始化为0
space = 0
digit = 0
others = 0
For i = 1 To Len(InputStr) '开始分别统计个数
CaseStr = Mid(InputStr, i, 1) '取得某个字符
Select Case CaseStr
Case "a" To "z", "A" To "Z" '如果字符是英文字母
letters = letters + 1
Case " " '如果字符是空格
space = space + 1
Case 0 To 9 '如果字符是数字
digit = digit + 1
Case Else '如果字符是其他字母
others = others + 1
End Select
Next
End Sub
Dim letters As Integer '声明模块级变量,此变量计算字母个数
Dim space As Integer '空格个数
Dim digit As Integer '数字个数
Dim others As Integer '其他字符个数
Private Sub Command1_Click()
Dim s As String
s = InputBox("请输入字符串")
Call jisuan(s)
'以下代码用来显示统计出的结果值
Print "字符串【" & s & "】包含:"
Print "英文字母数量="; letters
Print "空格数量="; space
Print "数字数量="; digit
Print "其他字符数量="; others
End Sub
Private Sub jisuan(InputStr As String) 'InputStr变量存储输入的字符串
Dim i As Integer '循环控制变量,整型
Dim CaseStr As String '此变量保存储所截取的字符
letters = 0 '初始化为0
space = 0
digit = 0
others = 0
For i = 1 To Len(InputStr) '开始分别统计个数
CaseStr = Mid(InputStr, i, 1) '取得某个字符
Select Case CaseStr
Case "a" To "z", "A" To "Z" '如果字符是英文字母
letters = letters + 1
Case " " '如果字符是空格
space = space + 1
Case 0 To 9 '如果字符是数字
digit = digit + 1
Case Else '如果字符是其他字母
others = others + 1
End Select
Next
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询