2012年11月5日 星期一

字串處理

給一個字串,請寫一個程式,計算此字串中,英文字元有幾個?
輸入說明:
輸入檔第一行表示有幾組測試資料,第二行開始的每一行即為一筆測試資料,每行最多
有1000 個字元。
輸出說明:
對每一筆測試資料,輸出字串中英文字元的個數。
輸入範例:
2
abc123def456
133adfag3428a2fwqgq2
輸出範例:
6
11

7 則留言:

  1. Dim mycount As Integer
    Dim strr 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, mycount
    For i = 1 To mycount
    Input #1, strr
    strr = Replace(strr, "0", "")
    strr = Replace(strr, "1", "")
    strr = Replace(strr, "2", "")
    strr = Replace(strr, "3", "")
    strr = Replace(strr, "4", "")
    strr = Replace(strr, "5", "")
    strr = Replace(strr, "6", "")
    strr = Replace(strr, "7", "")
    strr = Replace(strr, "8", "")
    strr = Replace(strr, "9", "")
    Print #2, Len(strr)
    Next
    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
    Input #1, x
    For i = 1 To x
    Line Input #1, y
    'MsgBox asc(a)
    n = Len(y)
    For j = 1 To n
    a = Asc(Mid(y, j, 1))
    If a >= 65 And a <= 90 Or a >= 97 And a <= 122 Then
    Sum = Sum + 1
    End If
    Next
    Print #2, Sum
    Sum = 0
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Dim s As Boolean
    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
    Line Input #1, x
    For j = 1 To Len(x)
    m = Asc(Mid(x, j, 1))
    s = True
    For k = 0 To 9
    If m = Asc(k) Then s = False: Exit For
    Next k
    If s = True Then t = t + 1
    Next j
    Print #2, t
    t = 0
    Next i
    Close
    Close
    End
    End Sub

    回覆刪除
  4. Bob&KiKi程式錯誤,要把中文也刪掉。
    復陞程式正確,也可以把字串用成同一大小寫,判斷會少兩個。
    另外標點符號也需要刪掉。

    回覆刪除
  5. Dim mycount, ans As Integer
    Dim strr 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, mycount
    Do
    Input #1, strr
    For i = 97 To 122
    ori = Len(strr)
    strr = Replace(strr, Chr(i), "")
    strr = Replace(strr, Chr(i - 32), "")
    ans = ans + ori - Len(strr)
    Next
    Print #2, ans: ans = 0
    Loop Until EOF(1)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  6. Dim s As Boolean
    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
    Line Input #1, x
    x = LCase(x)
    For J = 1 To Len(x)
    m = Asc(Mid(x, J, 1))
    s = True
    For K = Asc("a") To Asc("z")
    If m = K Then s = False: Exit For
    Next
    If s = False Then t = t + 1
    Next J
    Print #2, t
    t = 0
    Next i
    Close
    Close
    End
    End Sub

    回覆刪除