What is wrong with my version of strchr?

Posted by Eduard Saakashvili on Stack Overflow See other posts from Stack Overflow or by Eduard Saakashvili
Published on 2012-07-08T21:05:18Z Indexed on 2012/07/08 21:15 UTC
Read the original article Hit count: 143

Filed under:
|
|
|

My assignment is to write my own version of strchr, yet it doesn't seem to work. Any advice would be much appreciated. Here it is:

char *strchr (const char *s, int c) //we are looking for c on the string s
{

    int dog; //This is the index on the string, initialized as 0
    dog = 0;
    int point; //this is the pointer to the location given by the index
    point = &s[dog];
    while ((s[dog] != c) && (s[dog] != '\0')) { //it keeps adding to dog until it stumbles upon either c or '\0'
            dog++;
            }
    if (s[dog]==c) {
            return point; //at this point, if this value is equal to c it returns the pointer to that location
            }
    else {
            return NULL; //if not, this means that c is not on the string
            }
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about homework