How to count words in a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom E.

    How to count words in a string

    Hello, I would like to know what the most efficient way is to count
    words in a string and to determine what the average word length is. I
    know this can be done in a array, but is there an easier way using
    VB.Net? Thank you in advance!
  • One Handed Man [ OHM# ]

    #2
    Re: How to count words in a string

    Simple version.

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click

    Dim str As String = "ABC DEF GHIJ KLMNOPQ"

    Dim strA() As String = str.Split(" ")

    Dim TChrs As Int32

    Dim i As Int32

    For i = strA.Length - 1 To 0 Step -1

    TChrs += strA(i).Length

    Next

    MsgBox("Number of words = " & strA.Length)

    MsgBox("Average Length = " & TChrs / strA.Length)

    End Sub

    Regards - OHM






    Tom E. wrote:[color=blue]
    > Hello, I would like to know what the most efficient way is to count
    > words in a string and to determine what the average word length is. I
    > know this can be done in a array, but is there an easier way using
    > VB.Net? Thank you in advance![/color]

    Regards - OHM# OneHandedMan@BT Internet.com


    Comment

    • Cor

      #3
      Re: How to count words in a string

      Hi Tom,

      If you dont have to long strings I would first look what split would do for
      me.

      Something like this (did not test it, just my first thought)
      \\\\
      dim mycount() as string = split(mystring)
      myavarage = mystring.length / mycount.length
      ////

      This is the Microsoft.Visua l basic split, there is also a String.Split
      method with wich you can give an array of delimiters and a regex.split.

      If the string is very long than the split becomes slow, you can itterate
      through the string.
      (dont forget to compare characters than and not strings)

      I hope this helps,

      Cor


      "Tom E." <mtnbiker@centu rytel.net> schreef in bericht
      news:1398401c3c 24c$d87b2d00$a6 01280a@phx.gbl. ..[color=blue]
      > Hello, I would like to know what the most efficient way is to count
      > words in a string and to determine what the average word length is. I
      > know this can be done in a array, but is there an easier way using
      > VB.Net? Thank you in advance![/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: How to count words in a string

        * "One Handed Man [ OHM# ]" <OneHandedMan@& REMOVE&TO%MAIL% MEBTInternet.co m> scripsit:[color=blue]
        > Simple version.
        >
        > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
        > System.EventArg s) Handles Button1.Click
        >
        > Dim str As String = "ABC DEF GHIJ KLMNOPQ"
        >
        > Dim strA() As String = str.Split(" ")
        >
        > Dim TChrs As Int32
        >
        > Dim i As Int32
        >
        > For i = strA.Length - 1 To 0 Step -1
        >
        > TChrs += strA(i).Length
        >
        > Next
        >
        > MsgBox("Number of words = " & strA.Length)
        >
        > MsgBox("Average Length = " & TChrs / strA.Length)
        >
        > End Sub[/color]

        This method will not work with something like this: "Bla -- Foo" or "Foo---Bla". But
        this is a rare case and maybe can be "ignored".

        --
        Herfried K. Wagner [MVP]
        <http://www.mvps.org/dotnet>

        Comment

        • One Handed Man [ OHM# ]

          #5
          Re: How to count words in a string

          Thats why I said it was the 'Simple' version. Perhaps you could write
          something more comprehensive yourself Herfried, I think the group would be
          pleased to see some of your own work.

          Regrards - OHM#

          Herfried K. Wagner [MVP] wrote:[color=blue]
          > * "One Handed Man [ OHM# ]"
          > <OneHandedMan@& REMOVE&TO%MAIL% MEBTInternet.co m> scripsit:[color=green]
          >> Simple version.
          >>
          >> Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
          >> System.EventArg s) Handles Button1.Click
          >>
          >> Dim str As String = "ABC DEF GHIJ KLMNOPQ"
          >>
          >> Dim strA() As String = str.Split(" ")
          >>
          >> Dim TChrs As Int32
          >>
          >> Dim i As Int32
          >>
          >> For i = strA.Length - 1 To 0 Step -1
          >>
          >> TChrs += strA(i).Length
          >>
          >> Next
          >>
          >> MsgBox("Number of words = " & strA.Length)
          >>
          >> MsgBox("Average Length = " & TChrs / strA.Length)
          >>
          >> End Sub[/color]
          >
          > This method will not work with something like this: "Bla -- Foo" or
          > "Foo---Bla". But this is a rare case and maybe can be "ignored".[/color]

          Regards - OHM# OneHandedMan@BT Internet.com


          Comment

          • Tom E.

            #6
            Re: How to count words in a string

            Thanks for the ideas guys. I think this will help me out.

            Tom
            [color=blue]
            >-----Original Message-----
            >Thats why I said it was the 'Simple' version. Perhaps you could write
            >something more comprehensive yourself Herfried, I think the group[/color]
            would be[color=blue]
            >pleased to see some of your own work.
            >
            >Regrards - OHM#
            >
            >Herfried K. Wagner [MVP] wrote:[color=green]
            >> * "One Handed Man [ OHM# ]"
            >> <OneHandedMan@& REMOVE&TO%MAIL% MEBTInternet.co m>[/color][/color]
            scripsit:[color=blue][color=green][color=darkred]
            >>> Simple version.
            >>>
            >>> Private Sub Button1_Click(B yVal sender As System.Object, ByVal e[/color][/color][/color]
            As[color=blue][color=green][color=darkred]
            >>> System.EventArg s) Handles Button1.Click
            >>>
            >>> Dim str As String = "ABC DEF GHIJ KLMNOPQ"
            >>>
            >>> Dim strA() As String = str.Split(" ")
            >>>
            >>> Dim TChrs As Int32
            >>>
            >>> Dim i As Int32
            >>>
            >>> For i = strA.Length - 1 To 0 Step -1
            >>>
            >>> TChrs += strA(i).Length
            >>>
            >>> Next
            >>>
            >>> MsgBox("Number of words = " & strA.Length)
            >>>
            >>> MsgBox("Average Length = " & TChrs / strA.Length)
            >>>
            >>> End Sub[/color]
            >>
            >> This method will not work with something like this: "Bla -- Foo" or
            >> "Foo---Bla". But this is a rare case and maybe can be "ignored".[/color]
            >
            >Regards - OHM# OneHandedMan@BT Internet.com
            >
            >
            >.
            >[/color]

            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: How to count words in a string

              * "One Handed Man [ OHM# ]" <OneHandedMan@& REMOVE&TO%MAIL% MEBTInternet.co m> scripsit:[color=blue]
              > Thats why I said it was the 'Simple' version. Perhaps you could write
              > something more comprehensive yourself Herfried, I think the group would be
              > pleased to see some of your own work.[/color]

              Let's give the OP some homework. It was only a hint for the OP when
              your method won't calculate the right value.

              --
              Herfried K. Wagner [MVP]
              <http://www.mvps.org/dotnet>

              Comment

              • Cor

                #8
                Re: How to count words in a string

                Hi,

                My example has also an error I saw.

                Cor


                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: How to count words in a string

                  * "Cor" <non@non.com> scripsit:[color=blue]
                  > My example has also an error I saw.[/color]

                  ;-)

                  Maybe an improved algorithm for word counting can be found somewhere on
                  the web...

                  --
                  Herfried K. Wagner [MVP]
                  <http://www.mvps.org/dotnet>

                  Comment

                  • Cor

                    #10
                    Re: How to count words in a string

                    Hi Herfried,

                    But thinking it over, your example Foo--Bar is in the Dutch gramatic an
                    error so one word.
                    While Foo-Bar is normal Dutch gramatic and 1 word with 7 characters.

                    So the error from OHM is not really true, it depends on the used gramatic.

                    My error was that it had to be

                    (total length of character minis words) divided by words and I forgot the
                    amount of words to subract from the total length of characters.

                    :-)) Find if you can find an error in this?

                    Cor




                    "


                    Comment

                    • Herfried K. Wagner [MVP]

                      #11
                      Re: How to count words in a string

                      * "Cor" <non@non.com> scripsit:[color=blue]
                      > But thinking it over, your example Foo--Bar is in the Dutch gramatic an
                      > error so one word.
                      > While Foo-Bar is normal Dutch gramatic and 1 word with 7 characters.
                      >
                      > So the error from OHM is not really true, it depends on the used gramatic.[/color]

                      ACK. In French, they write "ashfdjd : asdasd", where in German we would
                      write "asdasd: asdasd". There are many other examples.

                      --
                      Herfried K. Wagner [MVP]
                      <http://www.mvps.org/dotnet>

                      Comment

                      Working...