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 / C++ / MFC
  4. How to count Words in a string?

How to count Words in a string?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
3 Posts 2 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.
  • A Offline
    A Offline
    Alex Cramer
    wrote on last edited by
    #1

    Please, if somebody has ready solution (function for counting of words in a string), write it here. I just want to obtain number of words in LPSTR string. Thanks!

    A 1 Reply Last reply
    0
    • A Alex Cramer

      Please, if somebody has ready solution (function for counting of words in a string), write it here. I just want to obtain number of words in LPSTR string. Thanks!

      A Offline
      A Offline
      Alexandru Savescu
      wrote on last edited by
      #2

      You can use strtok. First define a string of separators and then use strtok to find all matching tokens and count them. Best regards, Alexandru Savescu

      A 1 Reply Last reply
      0
      • A Alexandru Savescu

        You can use strtok. First define a string of separators and then use strtok to find all matching tokens and count them. Best regards, Alexandru Savescu

        A Offline
        A Offline
        Alex Cramer
        wrote on last edited by
        #3

        Thanks! Here was example: * STRTOK.C: In this program, a loop uses strtok * to print all the tokens (separated by commas * or blanks) in the string named "string". */ #include #include char string[] = "A string\tof ,,tokens\nand some more tokens"; char seps[] = " ,\t\n"; char *token; void main( void ) { printf( "%s\n\nTokens:\n", string ); /* Establish string and get the first token: */ token = strtok( string, seps ); while( token != NULL ) { /* While there are tokens in "string" */ printf( " %s\n", token ); /* Get next token: */ token = strtok( NULL, seps ); } } Output A string of ,,tokens and some more tokens Tokens: A string of tokens and some more tokens

        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