2012年7月29日 星期日

96模擬 Problem 2 (字串處理

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

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

a9sj2k13ckdi7
ba429gcbgq1

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

3 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim ans As Long
    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
    ans = 0
    For j = 1 To Len(x)
    If Mid(x, j, 1) <= 9 And Mid(x, j, 1) >= 0 Then ans = ans + 1
    Next
    Print #2, ans
    Next
    Close
    Close
    End
    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
    Sum = 0
    Line Input #1, s
    For j = 1 To Len(s)
    If Mid(s, j, 1) >= 0 And Mid(s, j, 1) <= 9 Then Sum = Sum + 1
    Next j
    Print #2, Sum
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Dim a As String
    Private Sub Form_Load()
    Me.Hide
    Dim ans As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    For j = 1 To Val(x)
    Line Input #1, y
    ans = 0
    For i = 1 To Len(y)
    a = Mid(y, i, 1)
    If IsNumeric(a) Then ans = ans + 1
    Next i
    Print #2, ans
    Next j
    Close #2
    Close #1
    End
    End Sub

    回覆刪除