2010年2月10日 星期三

2010/02/10 取代字串

讀入任意長度字串(最多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原則

出自 VB99題  No.63

10 則留言:

  1. Dim x As String
    Dim ans(4) As String
    Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Open App.Path & "/out.txt" For Output As #2
    Input #1, x
    ans(0) = Len(x)
    For i = 1 To 3
    For j = 1 To Len(x)
    y = Mid(x, j, 1)
    If i = 1 Then
    If y = "4" Then
    ans(i) = ans(i) + "4*號"
    Else
    ans(i) = ans(i) + y
    End If
    End If
    If i = 2 Then
    If y = "3" Then
    ans(i) = ans(i) + "3+號"
    Else
    ans(i) = ans(i) + y
    End If
    End If
    If i = 3 Then
    If y = "5" Then
    ans(i) = ans(i) + "5?號"
    Else
    ans(i) = ans(i) + y
    End If
    End If
    Next j
    Next i
    For i = 0 To 3
    Print #2, i + 1 & ":" & ans(i)
    Next i

    Close #2
    Close #1
    End Sub

    回覆刪除
  2. Dim x As String
    Dim ans(4) As String
    Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Open App.Path & "/out.txt" For Output As #2
    Input #1, x
    ans(0) = Len(x)
    ans(1) = Replace(x, "4", "4*號")
    ans(2) = Replace(x, "3", "3+號")
    ans(3) = Replace(x, "5", "5?號")
    For i = 0 To 3
    Print #2, i + 1 & ":" & ans(i)
    Next i
    Close #2
    Close #1
    End Sub
    用了Replace變的好快...

    回覆刪除
  3. Dim ans1, ans2, ans3 As String
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    n = Len(x)
    For i = 1 To n
    a = Mid(x, i, 1)
    If a = "4" Then
    ans1 = ans1 & "4*號"
    Else
    ans1 = ans1 & a
    End If
    Next i
    For i = 1 To n
    a = Mid(x, i, 1)
    If a = "3" Then
    ans2 = ans2 & "3+號"
    Else
    ans2 = ans2 & a
    End If
    Next i
    For i = 1 To n
    a = Mid(x, i, 1)
    If a = "5" Then
    ans3 = ans3 & "5?號"
    Else
    ans = ans3 & a
    End If
    Next i
    Print "(1)" & n
    Print "(2)" & ans1
    Print "(3)" & ans2
    Print "(4)" & ans3
    Close #1
    Close #2
    End Sub
    想不到還有這個函數 以後來試試看@@

    回覆刪除
  4. 高仔好,
    你的程式和你寫的輸出範例,不同哦。
    有沒有()呢?
    還有,你怎麼會用到雙迴圈呢?這不是單迴圈,就能解決的嗎?

    阿瑋好,
    你用的是單迴圈,可是,你不覺得這幾個單迴圈「超類似」嗎?
    應該也是用成一個迴圈就行了的吧?

    replace這個函數,設定是如何呢? 只會找出第一個來更換,還是,「每一個」呢?

    回覆刪除
  5. replace 字串中只要出現的每一個字都能替代
    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, x
    Close #1
    Print #2, Len(x)
    Print #2, Replace(x, "4", "4*號")
    Print #2, Replace(x, "3", "3+號")
    Print #2, Replace(x, "5", "5?號")
    Close #2
    End Sub

    回覆刪除
  6. 果然百練成鋼,這該是最精簡版了吧。無趣的可怕。

    回覆刪除
  7. ㄚ阿...那個() 是我程式做一做忘了加上去
    粗心= ="

    回覆刪除
  8. Dim x As String
    Dim ans(4) As String
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Input #1, x
    ans(1) = Len(x)
    strAns = x
    For i = 1 To Len(x)
    strX = Mid(x, i, 1)
    Select Case strX
    Case Is = "4"
    ans(2) = Mid(x, 1, i - 1) & "4*號" & Mid(x, i + 1, Len(x))
    Case Is = "3"
    ans(3) = Mid(x, 1, i - 1) & "3+號" & Mid(x, i + 1, Len(x))
    Case Is = "5"
    ans(4) = Mid(x, 1, i - 1) & "5?號" & Mid(x, i + 1, Len(x))
    End Select
    Next i
    Close #1
    Open App.Path & "\out.txt" For Output As #1
    For i = 1 To 4
    Print #1, ans(i)
    Next i
    Close #1
    End Sub

    回覆刪除
  9. 小白好,
    這題解得不錯哦。沒有用上面其它人的方式,想想自己的不同,果然能表現出一點兒企圖心,很好。再加油。

    回覆刪除
  10. Public 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, Len(x)
    Print #2, Replace(x, "4", "4*號")
    Print #2, Replace(x, "3", "3+號")
    Print #2, Replace(x, "5", "5?號")
    Close #2
    Close #1
    End
    End Sub

    2M12

    回覆刪除