Thread: can u solve it?

  1. #16
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    cheers hammer, brad.

  2. #17
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    after putting this code into my programme:

    printf("\n\nOk, please enter a word to look for in this sentence.\n");

    scanf("%s", &word);


    char *delims = { " .," };
    char *ptr;
    int count = 0;

    ptr = strtok(sentence2, delims);

    while (ptr != NULL)
    {
    if (strcmp(sentence2, word) ==0)


    count++;
    ptr = strtok(NULL, delims);
    }

    printf("Found word count %d times\n", count);
    }



    it seems only to count the word entered if its at the start of the sentence, and then counts every word after it. otherwise if it isnt the starting word it just counts a total of 0. how come?

    thanks, brad.
    Last edited by brad123; 04-29-2002 at 12:05 PM.

  3. #18
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by brad123

    Code:
            while (ptr != NULL)
            {
            if (strcmp(sentence2, word) ==0) 
    
            
            count++;
    You're comparing the wrong values in strcmp. Use strcmp(ptr, word).

    The idea of strtok in this implementation is that you get a pointer (ptr) to each word in the sentence, and process it one by one.

    Also, you best read about code tags if you're gonna post code here:
    http://www.cprogramming.com/cboard/m...&action=bbcode
    Last edited by Hammer; 04-29-2002 at 12:51 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. solve efficiently
    By jack_carver in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 07:56 AM
  2. Riddles to solve
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-11-2006, 04:07 PM
  3. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  4. Help to solve this problem
    By Romashka in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 09:32 AM