Results 1 to 7 of 7

Thread: ****-----Counting Words!!!!----****

  1. #1

    Thread Starter
    Lively Member mykg4orce's Avatar
    Join Date
    Oct 2000
    Location
    CANADA
    Posts
    92

    Exclamation

    this is the code i have to count words from a text field
    the text field has 72 words but this code keeps returning 73, i know that i can minus 1 from the counter to give the right answer but that is cheap, please tell me if there is a easier way of doing this, i am using VB version 5.0


    Option Explicit
    Dim LineOfText, SingleCharacter As String
    Dim i As Integer
    Dim Counter As Integer

    Counter = 0

    For i = 1 To Len(LineOfText)
    SingleCharacter = Mid$(LineOfText, i, 2)
    If SingleCharacter = " " Then
    Counter = Counter + 1
    Else
    SingleCharacter = Mid$(LineOfText, i, 1)
    If SingleCharacter = " " Then
    Counter = Counter + 1
    i = i + 1
    End If
    End If
    Next i

    lblTotalWords.Caption = Counter

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    I find that to be reverse of what happens for me.
    I get one less than the count of words. If there are 8 words I get sever so you need to add one to count and the reason you have to do that is because there is no " " at the end of the string but there is a word between the last " " and the end of the string.

    You also don't need the i = i + 1 as the loop self increments.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    I forgot..if you lhave any double spaces your count will be wrong as well.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4

    Thread Starter
    Lively Member mykg4orce's Avatar
    Join Date
    Oct 2000
    Location
    CANADA
    Posts
    92

    so what should be done??

    so what should be done??

  5. #5

  6. #6

    Thread Starter
    Lively Member mykg4orce's Avatar
    Join Date
    Oct 2000
    Location
    CANADA
    Posts
    92

    OH IT DOES!!!!!!

    thanks a lot it works perfectly, but i had posted that earlier so i thought i might as well try to figure out what was wrong with my code

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    mykg4orce
    Use the split funtion as recommended in your second post as it doesn't have that problem.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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