Write the function my_strchr(). The function has two parameters: a const char * s pointing to the first character in a C-style string, and a char c. Return a pointer to the first appearance of c appearing inside s and NULL (0) if c does not appear inside s.

Is there something wrong with this code? It doesn't return any output when i test it with the string "adabadoo" and char 'a'

const char * my_strchr(const char * s, char c)
{
    
    for(int i = 0; i < strlen(s); i++)
    {
        if(*s == c) return s;
        else return '\0';
        s++;
    }
}

Dani AI

Generated

The actual bug in ’s original snippet is logical: the function returns from inside the loop on the first iteration (the else causes an early return) so the string is never scanned past the first character. The code also mixed an index-style loop that calls strlen(s) each iteration with pointer arithmetic (*s / s++), which is redundant. For the test string "Abadabadoo!" note that the first character is uppercase 'A', so searching for lowercase 'a' will correctly match a later character — the search is case-sensitive.

Returning and printing the result needs care. Returning '\0' from a function declared to return const char * is confusing (it ends up as a null pointer constant) — prefer nullptr (C++11+) or NULL/0 in older code to make intent clear. Also, streaming a null const char * into std::cout is undefined behavior, so check the return before printing. A simple, safe test idiom:

const char *p = my_strchr(cstr, 'a');
if (p)
    std::cout << p << '\n';
else
    std::cout << "not found\n";

Passing a null pointer to the stream insertion operator is not defined; prefer explicit checks and clear null-return values (e.g. nullptr). (en.cppreference.com)

Looping advice and micro‑tips: scan until the terminator rather than calling strlen() on every iteration — calling strlen() repeatedly can make the routine do a lot of extra work (worst-case O(n^2) behavior for long strings). In idiomatic C/C++ a pointer-scan (while (*s) { if (*s==c) return s; ++s; }) is clean and efficient. Use pre-increment (++it / ++s) as a habit in generic C++ because post-increment may force an unnecessary temporary for user-defined iterators; for raw pointers/ints modern compilers usually optimize both forms. (cprogramming.com)

A final note: this routine already exists as strchr in the C standard library (<string.h> / <cstring>), which returns a pointer to the first match or NULL if not found — using the standard function avoids reimplementing a well-tested behavior unless the exercise is for learning. When testing, always confirm the input is null-terminated and check the returned pointer before dereferencing or printing. (en.cppreference.com)

Recommended Answers

All 5 Replies

delete line 7 and move the return between lines 9 and 10. That else statement on line 7 is causing the function to return after the virst attempt to chedk the value of *s.

delete line 7 and move the return between lines 9 and 10. That else statement on line 7 is causing the function to return after the virst attempt to chedk the value of *s.

That was what I did originally but it still doesn't return any thing =?
I test it with the code my professor gave me.

char cstr[50] = "Abadabadoo!";
cout << "my_strchr(cstr, 'a') expects adabadoo!" << endl;
cout << "  -- " << my_strchr(cstr, 'a') << endl;
const char * my_strchr(const char * s, char c)
{
    
    while(*s != '\0')
    {
        if(*s == c) 
            return s;
        ++s;
    }
    return '\0';
}

Can you explain why it is ++i and not i++ ? =?

In this code it could be either one -- just a matter of programmer preference. Use which ever one you want because the outcome will be the same. I like to use ++i because with some compilers the code might be just a couple nanoseconds faster than i++.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.