VB計算含有a,o,u的單字有幾個

2014-01-04 11:22 pm
例如 we are the world. We are the children.

are , world , are 三個單字含有a,o,u

結果輸出 3

另外

輸入 I am a teacher 將其反轉

結果輸出 teacher a am I

回答 (6)

2014-01-05 1:18 am
✔ 最佳答案
1.計算含有a,o,u 單字
Dim s As String = "we are the world. We are the children."
Dim a() As String
Dim i As Integer
Dim w, x, y, z As Integer
a = Split(s, " ")
For i = 0 To a.Length - 1
w = InStr(a(i), "a")
x = InStr(a(i), "o")
y = InStr(a(i), "u")
If w > 0 Or x > 0 Or y > 0 Then
z = z + 1
End If
Next
Debug.Print("含有 a,o,u 三個字元的單字共有 " & z & " 個")


2.輸入 I am a teacher 將其反轉
Dim s As String
Dim a() As String
Dim b As String
Dim i As Integer
s = InputBox("請輸入所要反轉的文字")
a = Split(s, " ")
For i = a.Length - 1 To 0 Step -1
b = b & a(i) & " "
Next
Debug.Print(b)

2014-01-04 17:20:26 補充:
第二題如果不是自由輸入則可刪除 s = InputBox 那列,

並把 Dim s As String 改成

Dim s As String = "I am a teacher"

2014-01-05 13:33:21 補充:
我沒用過 Strreverse 函數, 不過剛剛試了, 發現輸出結果是以字母為單位反轉, 而不是版大要的以單字為單位的反轉, 因此可能不適在本題.

2014-01-06 11:30:53 補充:
我覺得樓上OrAnGe*韋 的寫法不錯, 用 UBound 才是常見的寫法, 所以第二題我也抄他修改如下

Dim s As String = "I am a teacher"
Dim a() As String
Dim b As String
Dim i As Integer
a = Split(s, " ")
For i = UBound(a) To 0 Step -1
b = b & a(i) & " "
Next
Debug.Print(b)
2014-01-08 5:36 am
2014-01-06 6:49 am
1.
Dim s As String = LCase(InputBox("輸入要檢查的字串"))
Dim sp() As String = s.Split(" ")
Dim ans As Integer = 0
For Each i As String In sp
If i.Contains("a") Or i.Contains("o") Or i.Contains("u") Then ans += 1
Next
MsgBox(ans)
2.
Dim s As String = InputBox("輸入要檢查的字串")
Dim sp() As String = s.Split(" ")
Array.Reverse(sp)
MsgBox(Join(sp, " "))
參考: me
2014-01-06 4:29 am
沒記錯這是Q3集訓的題目
其實I am a teacher這樣寫就好了
Dim n As String, a As String, s
n = "I am a teacher"
s = Split(n)
For i = 0 To UBound(s)
a = s(i) & " " & a
Next
Debug.Print(a)
2014-01-05 4:18 pm
Strreverse我記得這是自串反轉的函式

2014-01-05 14:21:08 補充:
哦,沒注意到發問是要用單字反轉不是全部反轉,我癡呆了哈哈..
2014-01-05 7:23 am
1.計算含有a,o,u 單字
Dim s As String = "we are the world. We are the children."
Dim a() As String
Dim i As Integer
Dim w, x, y, z As Integer
a = Split(s, " ")
For i = 0 To a.Length - 1
w = InStr(a(i), "a")
x = InStr(a(i), "o")
y = InStr(a(i), "u")
If w > 0 Or x > 0 Or y > 0 Then
z = z + 1
End If
Next
Debug.Print("含有 a,o,u 三個字元的單字共有 " & z & " 個")


2.輸入 I am a teacher 將其反轉
Dim s As String
Dim a() As String
Dim b As String
Dim i As Integer
s = InputBox("請輸入所要反轉的文字")
a = Split(s, " ")
For i = a.Length - 1 To 0 Step -1
b = b & a(i) & " "
Next
Debug.Print(b)
2014-01-04 17:20:26 補充
第二題如果不是自由輸入則可刪除 s = InputBox 那列,

並把 Dim s As String 改成

Dim s As String = "I am a teacher"


收錄日期: 2021-04-16 16:21:05
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20140104000015KK02686

檢視 Wayback Machine 備份