2011年10月12日 星期三

96模擬 Problem 2 (字串處理

Problem 2 (字串處理 10%)
輸入一段字串(String),請寫出一個程式,計算此字串中阿拉伯數的字元有幾個?

輸入說明:第一列為要計算的列數,第二列及以後就是需要計算的字串。(請參照輸入範例)
輸入範例:test2.txt
2
a9sj2k13ckdi7
ba429gcbgq1

輸出說明:對每一測試資料,輸出字串中阿拉伯數字的個數。(請參照輸出範例)
輸出範例:result2.txt
5
4

3 則留言:

  1. Dim X$
    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, n
    For i = 1 To n
    Input #1, X
    Call Ans(X)
    Next
    Close
    Close
    End
    End Sub

    Sub Ans(a)
    Dim b
    b = 0
    For i = 1 To Len(a)
    m = Mid(a, i, 1)
    If m >= 0 And m <= 9 Then b = b + 1
    Next

    Print #2, b
    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
    Input #1, n

    For i = 1 To n
    Input #1, X
    Print #2, c(X)
    Next i

    Close #2
    Close #1
    End
    End Sub

    Function c(A)
    ans = 0
    For i = 1 To Len(A)
    M = Mid(A, i, 1)
    If M >= "0" And M <= "9" Then ans = ans + 1
    Next i
    c = ans

    End Function

    回覆刪除
  3. 佑好,arro好,
    程式正確。

    回覆刪除