برنامه پالیندروم در ویژوال بیسیک 6

بازدید29.8kپست ها1آخرین فعالیت10 سال پیش
0
0

پالیندروم یعنی رشته ای که اگر اون رو از هر طرف بخونی همون میشه مثل 35653

خوب برای این کار ما رشته رو برعکس میکنیم و اون رو با رشته اول مقایسه میکنیم اگر مثل هم بودن پس پالیندرومه!

Private Sub Command1_Click()
On Error Resume Next
       Dim inputWord As String
       Dim testWord As String
       Dim reverseWord As String
       Dim testChar As String
       Dim i As Integer
       Dim maxIndex As Integer
       maxIndex = 0
       i = 1
       '--Get input from the user.
       inputWord = InputBox("Please enter your word")
     
       '--Record the input in lower case and remove spaces (you can
       '--add checks for tabs, linefeeds, etc).
       maxIndex = Len(inputWord)
       For j = 0 To maxIndex
          testChar = Mid(inputWord, j, 1)
          If testChar <> " " Then
             testWord = testWord & LCase(testChar)
             
          End If
          
       Next
       Print testWord;
     
       '--Reverse testWord.
       maxIndex = Len(testWord)
       For i = maxIndex To 0 Step -1
          reverseWord = reverseWord & Mid(testWord, i, 1)
       Next
       Print " = " & reverseWord & "?"
     
       '--Compare reverseWord to testWord and output the results.
       If testWord = reverseWord Then
          MsgBox "Yes, it's a palindrome"
          Cls
       Else
          MsgBox "No, it's not a palindrome"
          Cls
       End If
End Sub

روش دیگه: تو کد بالا ما کد برعکس کردن رشته رو هم نوشتیم اما این تابع بصورت اماده در Vb6 وجود داره

Private Sub Command1_Click()
    Dim inputWord As String
    Dim reverseWord As String
    
    inputWord = InputBox("Please enter your word")
    reverseWord = StrReverse(inputWord)
    If inputWord = reverseWord Then
        MsgBox "Yes, it's a palindrome"
        Cls
    Else
        MsgBox "No, it's not a palindrome"
        Cls
    End If
End Sub
0

بسیار ممنون :smile12:

سوال برنامه نویسی دارید؟

ندونستن عیب نیست، نپرسیدن چرا!

خوش آمدید

برای طرح سوال، ایجاد بحث و فعالیت در سایت نیاز است ابتدا وارد حساب کاربری خود شوید. در صورتی که هنوز عضو سایت نیستید میتوانید در عرض تنها چند ثانیه ثبت نام کنید.