使用VB程序设计使用while语句对输入字符的数字个数和字母个数(如果有字母,统一转化为小写)进行统计的示例代码:
时间: 2023-05-31 19:03:31 浏览: 95
vb.net实现统计字符
Private Sub CountChars()
Dim strInput As String
Dim intNumCount As Integer
Dim intLetterCount As Integer
Dim intIndex As Integer
'获取输入字符串
strInput = InputBox("请输入字符串:")
'统计数字个数和字母个数
intIndex = 1
While intIndex <= Len(strInput)
'判断当前字符是否为数字
If IsNumeric(Mid(strInput, intIndex, 1)) Then
intNumCount = intNumCount + 1
Else
'转化为小写字母
If Asc(Mid(strInput, intIndex, 1)) >= 65 And Asc(Mid(strInput, intIndex, 1)) <= 90 Then
intLetterCount = intLetterCount + 1
Mid(strInput, intIndex, 1) = Chr(Asc(Mid(strInput, intIndex, 1)) + 32)
End If
End If
intIndex = intIndex + 1
Wend
'输出统计结果
MsgBox "数字个数为:" & intNumCount & vbCrLf & "字母个数为:" & intLetterCount & vbCrLf & "转化后的字符串为:" & strInput
End Sub
阅读全文