2010年3月5日 星期五

2010/03/05 消除字串

試寫一程式,以輸入一字串,其長度 (含空白), 最長為81個字元,然後檢驗該語言串中之母音 (A,E,I,O,U,a,e,i,o,u),並將之刪除,且該 些母音之位置由後續之字元向前位移予以佔用而不留下空白,例:輸入
"Mary^Lives^IN^300,^Born^St.,^Chungli,^Taiwan,^R.^O.^C." 其中"^" 表示空一個位置。
經刪除母音後成為 "Mry^Lvs^N^300,^Brn^St.,^Chngl,^Twn,^R.^.^C." ;然後將輸入之字串與刪除母音後之字串同時列印於報表。



測試資料:(請將下列文章存於檔案中,以讀檔案的方式來設計程式)

" He Sells Sea Shells by the Seashore."

" I don't know how to Complete the testing Program."

" The men who spead ill of others will take no

advantage of others in the last."

6 則留言:

  1. D im ans As String
    Dim xxx As String
    Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Open App.Path & "/out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, n
    ans = ""
    xxx = "AEIOUaeiou"
    For j = 1 To Len(n)
    q = Mid(n, j, 1)
    f = True
    For i = 1 To 10
    If q = Mid(xxx, i, 1) Then f = False
    Next i
    If f = True Then ans = ans + q
    Next j
    Print #2, ans
    Loop
    Close #2
    Close #1
    End Sub

    輸入:
    " He Sells Sea Shells by the Seashore."
    " I don't know how to Complete the testing Program."
    " The men who spead ill of others will take no advantage of others in the last."
    輸出:
    H Slls S Shlls by th Sshr.
    dn't knw hw t Cmplt th tstng Prgrm.
    Th mn wh spd ll f thrs wll tk n dvntg f thrs n th lst.

    先判斷是否為母音
    否就貼入答案中
    輸出頗為亂
    但檢查了一下應該是正確的

    回覆刪除
  2. Private Sub Form_Load()
    Dim mo(5) As String
    mo(1) = "a": mo(2) = "e": mo(3) = "i": mo(4) = "o": mo(5) = "u"
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do Until EOF(1)
    Input #1, x
    For i = 1 To Len(x)
    For j = 1 To 5
    x = Replace(LCase(x), mo(j), "")
    Next j
    Next i
    Print #2, x
    Loop
    Close #1
    Close #2
    End Sub
    老師 我記得有一個函數能把空格" " 刪掉
    是捨嚜函數呢??

    回覆刪除
  3. 高仔好,
    你的程式ok,你這次不用replace了,也OK啦。

    Y揚好,
    你用了replace,程式ok, 但是,replace 似乎可以更簡單地使用,讓這個程式更簡短吧?
    要去空白,用replace這個函數也成吧?

    回覆刪除
  4. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    aStr = "AEIOUaeiou"
    While Not EOF(1)
    Input #1, x
    For i = 1 To 10
    x = Replace(x, Mid(aStr, i, 1), "")
    Next i
    Print #2, x
    Wend
    Close #1
    Close #2
    End Sub

    回覆刪除
  5. 阿瑋好,
    就是這樣一次將所有的A都去掉,OK的。

    回覆刪除
  6. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    txt = "aeiouAEIOU"
    Do While Not EOF(1)
    Line Input #1, strQ
    k = Len(strQ)
    For i = 1 To Len(strQ)
    x = Mid(strQ, i, 1)
    For j = 1 To 10
    t = Mid(txt, j, 1)
    If x = t Then
    strQ = Mid(strQ, 1, i - 1) & Mid(strQ, i + 1, k - i - 1)
    k = k - 1
    End If
    Next j
    Next i
    Print #2, strQ
    Loop
    Close #2
    Close #1
    End Sub

    老師
    這題我有一點小問題
    就是
    不知道為什麼
    程式再分辨a的部分
    會跳過去說
    其他的母音都削掉了
    雖然部分的a會被削掉
    但大部分的都沒有
    看了很久 還是不知道錯在哪

    回覆刪除