精选问答
请教VB的一个题目我初学VB有个题目不会做,有人能帮帮我么?谢谢!有一个英语单词保存在变量Word中,通过键盘输入一个字符串,判断该字符串是不是Word的子串,如果是则给出子串在Word中的起始位置,然后停止查找。例如Word=\"preposition\",则sit就是Word的一个子串,其起始位置为6。(要求使用Do语句,在程序中不允许使用InStr函数)

2019-06-17

请教VB的一个题目我初学VB有个题目不会做,有人能帮帮我么?谢谢!有一个英语单词保存在变量Word中,通过键盘输入一个字符串,判断该字符串是不是Word的子串,如果是则给出子串在Word中的起始位置,然后停止查找。例如Word=\"preposition\",则sit就是Word的一个子串,其起始位置为6。(要求使用Do语句,在程序中不允许使用InStr函数)
优质解答
Sub Main() Dim word As String = "Iamverygood" Console.WriteLine("需要匹配的单词为:" + word) Console.WriteLine("请输入匹配的单词或词组以换行结束:") Dim word1 As String = Console.ReadLine() Dim array(word.Length - 1) As String Dim array1(word1.Length - 1) As String For i As Integer = 1 To word.Length array(i - 1) = Mid(word, i, 1) Next For j As Integer = 1 To word1.Length array1(j - 1) = Mid(word1, j, 1) Next Dim flag As Boolean = False Dim flagCount As Integer = 0 For k As Integer = 0 To array.GetUpperBound(0) If array(k) = array1(0) Then flag = True flagCount = k + 1 For n As Integer = 1 To array1.GetUpperBound(0) If array(k + n) array1(n) Then flag = False flagCount = k + n + 1 Exit For End If Next End If Next If flag = True Then Console.WriteLine("成功匹配,位置" + flagCount.ToString + "个") Else Console.WriteLine("没有匹配的!") End If Console.ReadLine() End Sub Sub Main() Dim word As String = "Iamverygood" Console.WriteLine("需要匹配的单词为:" + word) Console.WriteLine("请输入匹配的单词或词组以换行结束:") Dim word1 As String = Console.ReadLine() Dim array(word.Length - 1) As String Dim array1(word1.Length - 1) As String For i As Integer = 1 To word.Length array(i - 1) = Mid(word, i, 1) Next For j As Integer = 1 To word1.Length array1(j - 1) = Mid(word1, j, 1) Next Dim flag As Boolean = False Dim flagCount As Integer = 0 For k As Integer = 0 To array.GetUpperBound(0) If array(k) = array1(0) Then flag = True flagCount = k + 1 For n As Integer = 1 To array1.GetUpperBound(0) If array(k + n) array1(n) Then flag = False flagCount = k + n + 1 Exit For End If Next End If Next If flag = True Then Console.WriteLine("成功匹配,位置" + flagCount.ToString + "个") Else Console.WriteLine("没有匹配的!") End If Console.ReadLine() End Sub
相关问答