// This function searches through str for the character chr.
// If the chr is found, it returns a pointer into str where
// the character was first found, otherwise nullptr (not found).
const char* findTheChar(const char str[], char chr)
{
for (int k = 0; str[k] != 0; k++)
if (str[k] == chr)
return &str[k];
return nullptr;
}
1..
const char* findTheChar(const string &str,char chr)
{
for (int k = 0; str.length(); k++)
if (str[k] == chr)
return &str[k];
return nullptr;
}
2..
const char* findTheChar(const string &str,char chr)
{
for (auto &ch : str) {
if (ch == chr)
return &ch;
}
return nullptr;
}
Comments
Leave a comment