Results 1 to 8 of 8

Thread: counting question marks and full stops.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Exclamation 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:
    1. Private Sub cmdsentences_Click()
    2.     Dim Sentence As String, Char As String
    3.     Dim Count As Integer, I As Integer
    4.     Count = 0
    5.     Sentence = Trim(txtInput.Text)
    6.     If Len(Sentence) > 0 Then
    7.         For I = 1 To Len(Sentence)
    8.             Char = Mid(Sentence, I, 1)
    9.             If Char = "." Then
    10.            
    11.                 Count = Count + 1
    12.             End If
    13.         Next I
    14.             Count = Count
    15.     End If
    16.     lblCountsentences.Caption = Count
    17. End Sub

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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:
    1. If Char = "." Or Char = "?"Then

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    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

  4. #4
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    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.
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: counting question marks and full stops.

    Here's another way.

    VB Code:
    1. Dim strSentences() As String
    2.     Dim intCount As Integer
    3.    
    4.     strSentences = Split(Text1.Text, ".")
    5.     intCount = intCount + UBound(strSentences)
    6.     strSentences = Split(Text1.Text, "?")
    7.     intCount = intCount + UBound(strSentences)
    8.     strSentences = Split(Text1.Text, "!")
    9.     intCount = intCount + UBound(strSentences)
    10.     MsgBox intCount

  6. #6

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: counting question marks and full stops.

    Courtesy of yrwyddfa and I, with a few slight modifications:

    VB Code:
    1. Declare Sub RtlMoveMemory Lib "kernel32" ( _
    2.     ByRef lpvDest As Any, _
    3.     ByRef lpvSrc As Any, _
    4.     ByVal cbLen As Long _
    5. )
    6.  
    7. Function InStr2CharCount( _
    8.     ByVal pszString As Long, _
    9.     ByRef pszFind1 As String, _
    10.     ByRef pszFind2 As String _
    11. ) As Long
    12. Static chBuf(1024)  As Byte
    13. Dim chSearchChar1   As Byte
    14. Dim chSearchChar2   As Byte
    15. Dim lStringLen      As Long
    16. Dim i               As Long
    17.  
    18.     RtlMoveMemory lStringLen, ByVal (pszString - 4), 4&
    19.     RtlMoveMemory chBuf(0), ByVal pszString, lStringLen
    20.  
    21.     chSearchChar1 = AscW(pszFind1)
    22.     chSearchChar2 = AscW(pszFind2)
    23.     For i = 0 To lStringLen Step 2
    24.         If (chBuf(i) = chSearchChar1) Or (chBuf(i) = chSearchChar2) Then _
    25.             InStr2CharCount = InStr2CharCount + 1
    26.     Next i
    27. End Function
    28.  
    29. ' Usage:
    30. MsgBox Instr2CharCount(StrPtr(Trim$(txtInput.Text)), ".", "?")

  8. #8
    Junior Member
    Join Date
    Jun 2005
    Posts
    22

    Thumbs up 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width