Thread: pointer proggy

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    Question pointer proggy

    I need to write an implementation for search that steps through the string comparing each character wit the given character. If it finds a match, it will return a pointer to the current position. If it reaches the end of the string without finding a match, it will return a null pointer.

    Can anyone help me do it?

    Here is my code so far:#include <stdio.h>

    char* search (char* s, int c) {

    /*code here*/


    }

    int main () {
    char quick_fox [] = "The quick brown fox jumps over the lazy dog.";
    char c;
    char* p;

    c = 'x';
    p = search(quick_fox, c);
    if (p) {
    printf("The substring that starts with %c is \"%s\"\n", c, p);
    } else {
    printf("No %c in \"%s\"\n", c, quick_fox);
    }

    c = 'X';
    p = search(quick_fox, c);
    if (p) {
    printf("The substring that starts with %c is \"%s\"\n", c, p);
    } else {
    printf("No %c in \"%s\"\n", c, quick_fox);
    }
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    what about the standard strchr function?

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The manual page: strchr

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You need a temporary pointer that points to the beginning of the string. You can increment the temporary pointer (++) and get the character to where the temporary pointer is pointing by placing a * before it (it's called dereferencing). Just loop through the string and stop when you find a match or when you are at the end of the string. Here a beginning of the search function. You only have to fill in the while and if statement and you're done.
    Code:
    char* search (char* s, int c) 
    {
        char *tmp = s;
    
        if(tmp != NULL)
        {
            while( /* while not end of string and no match with c */ )
                tmp++;
    
            if( /* if at end of string then return NULL pointer */ )
                tmp = NULL;
        }
        return tmp;
    }
    Here an example of dereferencing:
    Code:
    int main(void)
    {
        char s[] = "Hello world\n";
        char *tmp = s;
    
        while(*tmp != '\0') /* while not end of string */
        {
            printf("%c", *tmp); /* print character */
            tmp++;
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM