counting question marks and full stops.
Below is my coding for the counting of sentences in a paragraph of inputted text. So far though i can only get it to count full stops (eg. however many full stops there are there are sentences.). What if i had a question mark though, because that would be the end of a sentence too???? so how do i include question marks in the code below to count question marks AND full stops. (eg. however many full stops AND question marks there are sentences).
hope u caught my drift!!
VB Code:
Private Sub cmdsentences_Click()
Dim Sentence As String, Char As String
Dim Count As Integer, I As Integer
Count = 0
Sentence = Trim(txtInput.Text)
If Len(Sentence) > 0 Then
For I = 1 To Len(Sentence)
Char = Mid(Sentence, I, 1)
If Char = "." Then
Count = Count + 1
End If
Next I
Count = Count
End If
lblCountsentences.Caption = Count
End Sub
Re: counting question marks and full stops.
If it's formatted correctly, you could check for two spaces, which should be between the period and the next sentence. Same from a question mark.
Otherwise, change it to this:
VB Code:
If Char = "." Or Char = "?"Then
Re: counting question marks and full stops.
THANKYOU!!!!
we have the "or" inbetween but i didnt realise that you had to have char= twice!!
thanks!
it works in other words..could you tell :P
Re: counting question marks and full stops.
Don't forget sentences can finish with an exclamation point to!
And does it mater if people over puncuate??? (this would be 3 sentences to you code)
Just some things to consider.
Re: counting question marks and full stops.
Here's another way.
VB Code:
Dim strSentences() As String
Dim intCount As Integer
strSentences = Split(Text1.Text, ".")
intCount = intCount + UBound(strSentences)
strSentences = Split(Text1.Text, "?")
intCount = intCount + UBound(strSentences)
strSentences = Split(Text1.Text, "!")
intCount = intCount + UBound(strSentences)
MsgBox intCount
Re: counting question marks and full stops.
Oh, wait, "THANK YOU!!!!" is a valid sentence!
Re: counting question marks and full stops.
Courtesy of yrwyddfa and I, with a few slight modifications:
VB Code:
Declare Sub RtlMoveMemory Lib "kernel32" ( _
ByRef lpvDest As Any, _
ByRef lpvSrc As Any, _
ByVal cbLen As Long _
)
Function InStr2CharCount( _
ByVal pszString As Long, _
ByRef pszFind1 As String, _
ByRef pszFind2 As String _
) As Long
Static chBuf(1024) As Byte
Dim chSearchChar1 As Byte
Dim chSearchChar2 As Byte
Dim lStringLen As Long
Dim i As Long
RtlMoveMemory lStringLen, ByVal (pszString - 4), 4&
RtlMoveMemory chBuf(0), ByVal pszString, lStringLen
chSearchChar1 = AscW(pszFind1)
chSearchChar2 = AscW(pszFind2)
For i = 0 To lStringLen Step 2
If (chBuf(i) = chSearchChar1) Or (chBuf(i) = chSearchChar2) Then _
InStr2CharCount = InStr2CharCount + 1
Next i
End Function
' Usage:
MsgBox Instr2CharCount(StrPtr(Trim$(txtInput.Text)), ".", "?")
Re: counting question marks and full stops.
Its not a very good way of counting sentences. But rather number of occurances of fullstops, question marks and exclamation marks. To increase accuracy try counting a trailing space. Like count for ". " and "? " etc. And dont forget to add 1 to count for the last sentence. This way confusions made by sentences like "Thank you!!!" will be eliminated. Actually its just more accurate way not foll-proof. More accurate count requires more sophisticated and complex code.