Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Word_Counter

Word_Counter

Scheduled Pinned Locked Moved C#
csharphelpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sardonicus
    wrote on last edited by
    #1

    simply, i want to make a c# program that counts words in a sentence entered by the user. i know a method to count no. of characters in a certain sentence, but what about words, what's the idea ?! i'd appreciate it if anyone could help here.

    M 1 Reply Last reply
    0
    • S sardonicus

      simply, i want to make a c# program that counts words in a sentence entered by the user. i know a method to count no. of characters in a certain sentence, but what about words, what's the idea ?! i'd appreciate it if anyone could help here.

      M Offline
      M Offline
      Moon Boy
      wrote on last edited by
      #2

      string[] words = sentence.Split(' '); int count = words.Length; ?

      S 1 Reply Last reply
      0
      • M Moon Boy

        string[] words = sentence.Split(' '); int count = words.Length; ?

        S Offline
        S Offline
        sardonicus
        wrote on last edited by
        #3

        thnx a lot man. but am sorry i got another question, this two lines of code counts the number of words only in case they got only one space character between each two words, what about if i got a sentence like this "hello world" with 5 space characters for example and not "hello world" with one space character. the program won't count the first sentence as two words i think.

        E B 2 Replies Last reply
        0
        • S sardonicus

          thnx a lot man. but am sorry i got another question, this two lines of code counts the number of words only in case they got only one space character between each two words, what about if i got a sentence like this "hello world" with 5 space characters for example and not "hello world" with one space character. the program won't count the first sentence as two words i think.

          E Offline
          E Offline
          exhaulted
          wrote on last edited by
          #4

          Check for the length of each instance in the array If it has 1 character or more it is a word. Try something like the following string[] words = sentence.Split(' '); count = 0; for (int x = 0; x < words.Length; x++) { if (words[x].Length > 0) count++; } Kev

          S 1 Reply Last reply
          0
          • S sardonicus

            thnx a lot man. but am sorry i got another question, this two lines of code counts the number of words only in case they got only one space character between each two words, what about if i got a sentence like this "hello world" with 5 space characters for example and not "hello world" with one space character. the program won't count the first sentence as two words i think.

            B Offline
            B Offline
            Brian Delahunty
            wrote on last edited by
            #5

            int count = Regex.Replace("Hello      World", @"\s+", " ").Trim(' ').Split(' ').Length;

            That should work fine.. You can also have spaces at the front and the end of the string and it will still work. e.g.

            string testString = "     This    is  my Dog ";
            int count = Regex.Replace(testString, @"\s+", " ").Trim(' ').Split(' ').Length;

            Regards, Brian Dela :-) http://www.briandela.com IE 6 required.
            http://www.briandela.com/pictures Now with a pictures section :-D
            http://www.briandela.com/rss/newsrss.xml RSS Feed

            1 Reply Last reply
            0
            • E exhaulted

              Check for the length of each instance in the array If it has 1 character or more it is a word. Try something like the following string[] words = sentence.Split(' '); count = 0; for (int x = 0; x < words.Length; x++) { if (words[x].Length > 0) count++; } Kev

              S Offline
              S Offline
              sardonicus
              wrote on last edited by
              #6

              thx a lot guys

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups