Thread: Extracting words from strings

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Extracting words from strings

    Hi
    i want to extract all the words in a string, so that i can compare each word with another to find the most repeated word in the whole string.


    Anyone,, please help me

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Look into strtok() in string.h

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    Any example program??

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Check out this snippet from the faq.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There are a few functions (including strtok) you can use to split a string on spaces (ie, separate a sentance into words), or you can write your own. Have a look at this thread, which is a discussion of exactly that.

    In terms of finding duplicates: the straightforward, "brute-force" method will be fine unless you got your sentence out of James Joyce or David Foster Wallace: After you have split the string into an array, for each element in the array loop through the array using strcmp(). Use a same length, separate array of integers (ie, one for each word), all set to zero. When a duplicate is found, set the corresponding int to -1. When proceeding through the loop, skip elements that have a corresponding value of -1 (and, obviously, set the corresponding int to the number of finds at the end of the loop).
    Last edited by MK27; 04-15-2009 at 09:15 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing Words from a text file
    By matt_570 in forum C++ Programming
    Replies: 18
    Last Post: 12-10-2008, 12:35 PM
  2. how to extract words from a string
    By panfilero in forum C Programming
    Replies: 7
    Last Post: 11-04-2005, 08:06 AM
  3. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  4. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  5. extracting word-per-word from strings
    By tygernoot in forum C++ Programming
    Replies: 4
    Last Post: 12-27-2004, 02:39 PM