有下面的函数过程: Private Function fun(str As String, ch As String) As Integer n = 0 For k = 1 To Len(str) If Mid(str, k

admin2019-01-17  28

问题 有下面的函数过程:
Private Function fun(str As String, ch As String) As Integer
    n = 0
    For k = 1 To Len(str)
        If Mid(str, k, 1) = ch Then
            n = n + 1
        End If
    Next k
    fun = n
End Function
此函数返回的是

选项 A、字符ch在字符串str中第一次出现的位置
B、字符ch在字符串str中出现的次数
C、字符ch在字符串str中最后一次出现的位置
D、以上都是错误的

答案B

解析 字符串函数Len(str)返回字符串str长度;Mid(str, k, 1)表示从第K个字符开始向后截取一个字符,并与字符ch进行比较,若表达式返回True,则n的值加1,题目中函数返回n的值为字符ch在字符串str中出现的次数。故正确答案为选项B。
转载请注明原文地址:https://kaotiyun.com/show/1gFp777K
0

最新回复(0)