0

我对这个程序有点困惑。我是 Visual Basic 的新手,但对 C 来说是中级。实际上我想在不使用 Visual Basic 的库函数的情况下获取字符串的子字符串。这是我也给出了我的 VB 代码的 C 源代码。1.程序将从用户那里获得两个输入,即 A 和 B 2.然后从 B 中找到子字符串。3.最后打印结果。

int i,j=0,k=0,substr=0;
            for(i=0;i<strlen(a);i++)
            {

                if(a[i]==b[j])
                {
                    j++;

                    if(b[j]==0)
                    {
                        printf("second string is substring of first one");
                        substr=1;
                        break;
                    }
                }
            }
            for(i=0;i<strlen(b);i++)
            {
                if(b[i]==a[k])
                {
                    k++;
                    if(a[k]==0)
                    {
                        printf(" first string  is substring of second string");
                        substr=1;
                        break ;
                    }
                }
            }
            if(substr==0)
            {
                         printf("no substring present");
             }

虽然我的代码是

        Dim a As String
    Dim b As String
    a = InputBox("Enter First String", a)
    b = InputBox("Enter 2nd String", b)
        Dim i As Integer
        Dim j As Integer = 0
        Dim k As Integer = 0
        Dim substr As Integer = 0
        For i = 0 To a.Length - 1

            If a(i) = b(j) Then
                j += 1

                If b(j) = 0 Then
                MsgBox("second string is substring of first one")
                    substr = 1
                    Exit For
                End If
            End If
        Next i
        For i = 0 To b.Length - 1
            If b(i) = a(k) Then
                k += 1
                If a(k) = 0 Then
                MsgBox(" first string  is substring of second string")
                    substr = 1
                    Exit For
                End If
            End If
        Next i
        If substr = 0 Then
        MsgBox("no substring present")
        End If
End Sub

编译时会出现以下调试错误。

                                           Line Col
Error 1 Operator '=' is not defined for types 'Char' and 'Integer'. 17  24  
Error 2 Operator '=' is not defined for types 'Char' and 'Integer'. 27  24  
4

3 回答 3

2

您的部分困惑是 .Net 字符串不仅仅是字符缓冲区。我将假设您至少可以使用字符串。如果不能,请改用需要声明字符数组。顺便说一句,这应该可以让您以 1:1 的形式进行翻译:

Private Shared Function search(ByVal a As String, ByVal b As String) As Integer
    Dim i As Integer = 0
    Dim j As Integer = 0
    Dim firstOcc As Integer

    While i < a.Length

        While a.Chars(i)<>b.Chars(0) AndAlso i < a.Length
            i += 1
        End While

        If i >= a.Length Then Return -1 'search can not continue

        firstOcc = i

        While a.Chars(i)=b.Chars(j) AndAlso i < a.Length AndAlso j < b.Length
            i += 1
            j += 1
        End While

        If j = b.Length Then Return firstOcc
        If i = a.Length Then Return -1

        i = firstOcc + 1
        j = 0
    End While
    Return 0
End Function

Shared Sub Main() As Integer
    Dim a As String
    Dim b As String
    Dim loc As Integer

    Console.Write("Enter the main string :")
    a = Console.ReadLine()

    Console.Write("Enter the search string :")
    b = Console.ReadLine()

    loc = search(a, b)

    If loc = -1 Then
      Console.WriteLine("Not found")
    Else
      Console.WriteLine("Found at location {0:D}",loc+1)
    End If 

    Console.ReadKey(True)
End Sub

但请不要真正使用它。你真正需要的是:

Private Shared Function search(ByVal haystack as String, ByVal needle As String) As Integer
     Return haystack.IndexOf(needle)
End Function
于 2013-04-09T17:36:41.390 回答
1

VB 有一个名为 InStr 的内置函数,它是语言的一部分。它返回一个整数,指定一个字符串在另一个字符串中第一次出现的起始位置。

http://msdn.microsoft.com/en-us/library/8460tsh1(v=VS.80).aspx

皮特

于 2013-04-09T17:26:35.110 回答
1

在此处输入图像描述

试试这个,这将返回一个 List(Of Integer),其中包含在指定搜索起始位置之后源文本中所有出现的查找文本的索引。

Option Strict On
Public Class Form1
''' <summary>
''' Returns an array of indexes where the find text occurred in the source text.
''' </summary>
''' <param name="Source">The text you are searching.</param>
''' <param name="Find">The text you are searching for.</param>
''' <param name="StartIndex"></param>
''' <returns>Returns an array of indexes where the find text occurred in the source text.</returns>
''' <remarks></remarks>
Function FindInString(Source As String, Find As String, StartIndex As Integer) As List(Of Integer)
    If StartIndex > Source.Length - Find.Length Then Return New List(Of Integer)
    If StartIndex < 0 Then Return New List(Of Integer)
    If Find.Length > Source.Length Then Return New List(Of Integer)
    Dim Results As New List(Of Integer)
    For I = StartIndex To (Source.Length) - Find.Length
        Dim TestString As String = String.Empty
        For II = I To I + Find.Length - 1
            TestString = TestString & Source(II)
        Next
        If TestString = Find Then Results.Add(I)
    Next
    Return Results
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        Dim Search As String = "Hello world, this world is an interesting world"
        Dim Find As String = "world"
        Dim Indexes As List(Of Integer) = New List(Of Integer)
        Try
            Indexes = FindInString(Search, Find, 0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        RichTextBox1.Text = "Search:" & vbCrLf
        RichTextBox1.Text = RichTextBox1.Text & Search & vbCrLf & vbCrLf
        RichTextBox1.Text = RichTextBox1.Text & "Find:" & vbCrLf
        RichTextBox1.Text = RichTextBox1.Text & Find & vbCrLf & vbCrLf
        RichTextBox1.Text = RichTextBox1.Text & "-----------" & vbCrLf
        RichTextBox1.Text = RichTextBox1.Text & "Result Indexes:" & vbCrLf & vbCrLf
        For Each i As Integer In Indexes
            RichTextBox1.Text = RichTextBox1.Text & i.ToString & vbCr
        Next
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
End Class

这是另一种方式,不使用 .Net 函数。

Function FindInString(Source As String, Find As String, StartIndex As Integer) As Integer()
    If StartIndex > Len(Source) - Len(Find) Then Return {}
    If StartIndex < 0 Then Return {}
    If Len(Find) > Len(Source) Then Return {}
    Dim Results As Integer() = {}, ResultCount As Integer = -1
    For I = StartIndex To Len(Source) - Len(Find)
        Dim TestString As String = ""
        For II = I To I + Len(Find) - 1
            TestString = TestString & Source(II)
        Next
        If TestString = Find Then
            ResultCount += 1
            ReDim Preserve Results(ResultCount)
            Results(ResultCount) = I
        End If
    Next
    Return Results
End Function
于 2013-04-09T19:30:05.100 回答