2011年2月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原則



參考 http://chscvb.blogspot.com/2010/02/20100210.html

9 則留言:

  1. Dim a As String

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2

    Input #1, a

    For i = 1 To Len(a)

    ch1 = Mid(a, i, 1)
    If ch1 = 4 Then ch1 = "4*號"
    ans1 = ans1 & ch1

    ch2 = Mid(a, i, 1)
    If ch2 = 3 Then ch2 = "3+號"
    ans2 = ans2 & ch2

    ch3 = Mid(a, i, 1)
    If ch3 = 5 Then ch3 = "5?號"
    ans3 = ans3 & ch3

    Next

    Print #2, "(1):" & Len(a)
    Print #2, "(2):" & ans1
    Print #2, "(3):" & ans2
    Print #2, "(4):" & ans3

    Close #1
    Close #2

    End
    End Sub
    --------------

    剛去翻以前學長們寫的

    Replace函數

    這個晚點來研究看看 ~

    回覆刪除
  2. arro好,
    程式ok。
    用replace是更快的方式,也要學。

    回覆刪除
  3. Dim A As String


    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Input #1, A
    Close #1



    Open App.Path & "\out.txt" For Output As #2
    Print #2, Len(A)
    Print #2, Replace(A, "4", "4*號")
    Print #2, Replace(A, "3", "3+號")
    Print #2, Replace(A, "5", "5?號")

    Close #2

    End
    End Sub

    在補充知識那有字串處理涵數。
    學長推薦:)

    回覆刪除
  4. 佑好,
    程式正確,但是,注意一下,這個函數(不是涵數)是取代幾個呢? 一個還是全部呢?

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    Dim n As String
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Input #1, n
    m = n
    Print #2, Len(n)

    m = Replace(n, "4", "4*號")
    Print m#; 2,

    m = n
    m = Replace(n, "3", "3+號")
    Print #2, m

    m = n
    m = Replace(n, "5", "5?號")
    Print #2, m

    Close #1
    Close #2
    End
    End Sub

    回覆刪除
  6. 緣尉好,
    程式幾乎正確。 除了打錯字。太急了哦。

    回覆刪除
  7. Private Sub Form_Load()
    Me.Hide
    Dim n As String
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Input #1, n
    m = n
    Print #2, Len(n)

    m = Replace(n, "4", "4*號")
    Print #2, m

    m = n
    m = Replace(n, "3", "3+號")
    Print #2, m

    m = n
    m = Replace(n, "5", "5?號")
    Print #2, m

    Close #1
    Close #2
    End
    End Sub
    ------------------------------------
    -.-|||

    回覆刪除
  8. 緣尉好,
    三條線啊,沒關係,多做一些,在貼之前,注意一下執行結果,養成自己的sop(標準作業程序)。

    回覆刪除
  9. 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, n
    Call A1(n)
    Close
    Close
    End
    End Sub

    Sub A1(n)
    Print #2, Len(n)
    a = n
    a = Replace(a, "4", "4*號")
    Print #2, a
    a = n
    a = Replace(a, "3", "3+號")
    Print #2, a
    a = n
    a = Replace(a, "5", "5?號")
    Print #2, a
    End Sub


    2:07

    回覆刪除