That must be the most complicated
CharacterInString() I have ever seen.
Are you by any chance a Java programmer?
char CharacterInString (const char *s, char c)
{
for (s; *s; ++s)
if (*s == c) return c;
return 0;
}
Of course, one small change makes
strchr():
char const *strchr (char const *s, char c)
{
for (s; *s; ++s)
if (*s == c) return s;
return NULL;
}
So you may as well use that.
I might as well take the chance to opine a bit on the "sanity check", because sometimes they are a good thing. I don't think it's useful in this case, though. If the code calling this function passes it a NULL string by mistake, there's something seriously wrong with it. You want it to crash as close to the bug as possible. If the function goes ahead and returns a legal response, the parent function could go on its merry way for god knows how long before the error is discovered. Also, for MCU applications I'd rather be extra careful about things like that and save the space in my code memory rather than compile in branches that will never be followed.
For desktop applications, a better way would be to include assert.h and then use
assert(String != NULL). You can easily use the preprocessor to just drop all the assert() calls from the code if you're not compiling in debug mode, and it'll give you a nice line-number error message. If your device has any sort of debug interface, I'd recommend setting this up to receive assert() failures.