search number of times a substring apear in a string

This is a discussion on search number of times a substring apear in a string within the C Programming forums, part of the General Programming Boards category; I am having a hard time with this excercise. Can anyone give me a suggestion? thanks...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    30

    search number of times a substring apear in a string

    I am having a hard time with this excercise. Can anyone give me a suggestion?

    thanks

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    30
    I know how to do it now

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    chucked this together, not everything is declared but should give you an idea how to do it..

    Code:
    int i = 0;
    int numAppearances = 0;
    while (string[i] != '\0') {
            int j = 0;
    	while (j < substringLength) {
    		if (string[i++] == substring[j++]) {
                            if (j == substringLength) {
                                    numAppearances++;
                            }
    		}
                    else {
                            break;
                    }
            }
    }
    Last edited by space-oddity; 05-01-2008 at 05:33 AM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Or just strstr()... or strcmp(). Why are people scared of pointers!?

    Why bother with some thing that the standard library supplies?
    Last edited by zacs7; 05-01-2008 at 07:01 AM.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    Quote Originally Posted by zacs7 View Post
    Or just strstr()... or strcmp(). Why are people scared of pointers!?

    Why bother with some thing that the standard library supplies?
    for practice? the poster said it was an exercise

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Practice is derived from practical,

    It's practical to use the standard C functions. Hence, you practice practicallity.

    It's less code, and teaches you more IMO. If it's an assignment, which it appears to be -- unless otherwise stated it'd be a waste of time to reinvent the wheel.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. search numbers in an inputed string [array of strings]
    By zattara13 in forum C Programming
    Replies: 1
    Last Post: 03-28-2008, 07:11 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. Replies: 11
    Last Post: 12-11-2005, 11:50 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. how can i prove a string does not contain a number ?
    By blue_gene in forum C++ Programming
    Replies: 7
    Last Post: 04-07-2004, 10:35 PM

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