2012年11月5日 星期一

取代字串

讀入任意長度字串(最多50字元),對此字串坐下列處理動作: 
(1) 列印出字串長度(使用者輸入的部分)。 
(2) 以一個"4*號"字串取代每一個4字母單字, 
(3) 以一個"3+號"字串取代每一個3字母單字, 
(4) 以一個"5?號"字串取代每一個5字母單字, 
並列印新字串。

輸入範例:

Enernet 5-4-3原則

輸出範例:
(1):15
(2):Enernet 5-4*號-3原則
(3):Enernet 5-4-3+號原則
(4): Enernet 5?號-4-3原則

5 則留言:

  1. Dim str As String
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, str
    Print #2, "(1):" & Len(str)
    str1 = Replace(str, "4", "4*號")
    Print #2, "(2):" & str1
    str2 = Replace(str, "3", "3+號")
    Print #2, "(3):" & str2
    str3 = Replace(str, "5", "5?號")
    Print #2, "(4):" & str3
    Close #2
    Close #1
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim a, n As String
    Dim q As Boolean
    w = 0
    Input #1, a
    w = Len(a)
    Print #2, w
    For i = w To 1 Step -1
    q = False
    s = Mid(a, i, 1)
    If s = 4 Then
    l = Left(a, w - 5)
    r = Right(a, w - i)
    s = "4 *號"
    q = True
    End If
    If s = 3 Then
    l = Left(a, w - 3)
    r = Right(a, w - i)
    s = "3+號"
    q = True
    End If
    If s = 5 Then
    l = Left(a, w - 7)
    r = Right(a, w - i)
    s = "5?號"
    q = True
    End If
    If q = True Then Print #2, l & s & r
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, x
    Print #2, "(1):" & Len(x)
    Print #2, "(2):" & " " & Replace(x, "4", "4*號")
    Print #2, "(2):" & " " & Replace(x, "3", "3+號")
    Print #2, "(2):" & " " & Replace(x, "5", "5?號")
    Close
    Close
    End
    End Sub

    回覆刪除
  4. Bob & 復陞 程式正確。
    復陞 下次試試函數吧
    KiKi 忘了把(2) 改成 (3)、(4)

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, x
    Print #2, "(1):" & Len(x)
    Print #2, "(2):" & " " & Replace(x, "4", "4*號")
    Print #2, "(3):" & " " & Replace(x, "3", "3+號")
    Print #2, "(4):" & " " & Replace(x, "5", "5?號")
    Close
    Close
    End
    End Sub

    回覆刪除