strstr Question

This is a discussion on strstr Question within the C Programming forums, part of the General Programming Boards category; I'm reading through an array, and i wanted to count the number of occurences that a certain word appears for ...

  1. #1
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235

    Question strstr Question

    I'm reading through an array, and i wanted to count the number of occurences that a certain word appears for instance if the user wants to count how much times "the" appears in the array. Umm i came about the strstr function from the string library, but im not sure if it could be used for that. Can it?

    The aim is to count number of occurences of a word
    Last edited by Matus; 11-11-2008 at 04:07 PM.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why not? If you want word exactly, you should search for " the " (otherwise it would match "there" and "other" and "clothe") (or if you're worried about punctuation, just search for "the" and make sure either side is either a space or a quote or a punctuation or whatever).

  3. #3
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by tabstop View Post
    Why not? If you want word exactly, you should search for " the " (otherwise it would match "there" and "other" and "clothe") (or if you're worried about punctuation, just search for "the" and make sure either side is either a space or a quote or a punctuation or whatever).

    I was thinking something like this, cept i think my logic is crap based on it. Im reading that it returns a pointer to the current location from where the word begins. see

    Code:
             if(strstr(buffer,word)!=NULL){
             ++count;}
    was trying that but if its a pointer to the location, then i believe it would stop right at the first encounter right?
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Yes, you'll have to add some amount to the returned pointer and keep iterating through your string.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right. If only there was a way to make a statement (or group of statements) loop continuously while some condition is met....

  6. #6
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by tabstop View Post
    Right. If only there was a way to make a statement (or group of statements) loop continuously while some condition is met....

    Ok thanks. Good to know it can work, ill go work on those statements.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  2. strstr() question
    By blue_gene in forum C Programming
    Replies: 12
    Last Post: 04-15-2004, 11:22 PM
  3. Question About Finding a Word and Replacing It
    By Zildjian in forum C Programming
    Replies: 3
    Last Post: 09-23-2003, 09:50 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21