Thread: Searching for words within a sentence

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Montreal, Quebec

    Question Searching for words within a sentence

    Does anyone know how to search for words within a specific sentence using strings? Here's an example of what I mean.

    The word: back
    The sentence: I'll be back.
    I'm using visual c++ 6.0.

    Edit: or to check for words within a sentence.

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    go character by character?..or go by words...white space separate words (tabs, end-line, space)..
    nextus, the samurai warrior

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Look into the find member function.

  4. #4
    Registered User
    Join Date
    Apr 2002
    I'm with PJ.

    At this level, use find().

    Also, investigate 'basic_string'. The member functions that you'll find will surprise/amaze you.

    (Why they're not used more often is a mystery to me, though I understand the implicit rationale behind "grunt work". Learn the basics, then...appreciate the functionality of the language.)

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Registered User
    Join Date
    Feb 2002

    there is strtok()

    ok, in one of the headers, <iostream> or <cstdio> is the strtok() function

    it takes in 2 parameters
    1 - is the string you want to find words for
    2 - is a set that represents every deliminating character
    the deliminating character is over written by '\0' then str is passed back.

    char *strtok(char *str, const char *set);

    every subsequent call after the initial needs str to be passed NULL.

    char str[] = "Hello and Welcome back!";
    char set[] = "\\ \'\",.!?"; // and whatever else
    char *tok;

    tok = strtok(str, set);

    tok = strtok(NULL, set);

    This is because every subsequent call with NULL, tells strtok to start back where str left off... it then repeats and sends back another pointer... until str reaches the Null-Terminating character...

    Great function for parsing through sentences to find words.

    I reccomend always to write every function that you plan to use yourself, so that way you get a better understanding of the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector<...>::iterators and insert - I'm stumped.
    By Dino in forum C++ Programming
    Replies: 6
    Last Post: 12-25-2007, 06:11 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM