2011年6月9日 星期四

字串處理

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

6 則留言:

  1. 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 k = 1 To a
    Line Input #1, n
    ans = 0

    For i = 1 To Len(n)
    If Asc(Mid(n, i, 1)) >= 65 And Asc(Mid(n, i, 1)) <= 122 Then ans = ans + 1
    Next
    Print #2, ans

    Next
    Close
    Close
    End
    End Sub


    /////////
    輸入
    2
    abc123
    llllllllllllllIIIIIIIIIIIIII111111111111111

    輸出
    3
    28

    回覆刪除
  2. Private Sub Form_Load()
    Dim Times As Byte
    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
    Times = 0
    For j = 1 To Len(X)
    If Mid(X, j, 1) >= "A" And Mid(X, j, 1) <= "z" Then Times = Times + 1
    Next j

    Print #2, Times

    Next i
    Close #2
    Close #1


    /////////
    輸入
    2
    123AAAVCCSQWasdag
    l1IvAeji6國字

    輸出

    14
    7

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

    Input #1, n
    For ii = 1 To n
    Line Input #1, m
    c = 0
    For i = 1 To Len(m)
    If Asc(Mid(m, i, 1)) > 57 Then c = c + 1
    Next
    Print #2, c
    Next
    Close #1
    Close #2
    End
    End Sub
    ------------------------
    IN.TXT
    4
    abc123def456
    133adfag3428a2fwqgq2
    123AAAVCCSQWasdag
    l1IvAeji6國字
    ------------------------
    OUT.TXT
    6
    11
    14
    7

    回覆刪除
  4. arro,佑,緣尉好,
    你們三個都做一樣的方式,似乎是正確的。只是,
    你們都偷懶了,有錯誤的機會。
    要英文字,你們就用A~Z a~z來算,你們用的是A~z,ascii在這兩個中間沒有空了嗎?
    A-65
    Z-90
    a-97
    所以,91~96的ascii值的字,如果出來的話,是會出錯的哦。
    不能省的檢查,別省略。
    當然,不用的檢查,也別多做。

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    'x = Split("A Z a z")
    'For i = 0 To UBound(x)
    'Print Asc(x(i))
    'Next
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Input #1, n
    For ii = 1 To n
    Line Input #1, m
    c = 0
    For i = 1 To Len(m)
    If (Asc(Mid(m, i, 1)) >= 65 And Asc(Mid(m, i, 1)) <= 90) Or (Asc(Mid(m, i, 1)) >= 97 And Asc(Mid(m, i, 1)) <= 122) Then c = c + 1
    Next
    Print #2, c
    Next
    Close #1
    Close #2
    End
    End Sub

    回覆刪除
  6. 緣尉好,
    程式正確,但是,
    Asc(Mid(m, i, 1)
    這個動作重複做這麼多次,浪費時間,用個變數給它,只要做一次。

    回覆刪除