Write a function char findx const char s const char x that
| Write a function , char* findx (const char* s, const char* x), that |
| * finds the first occurrence of the C-style string x in s. This is using C++ |
| Write a function , char* findx (const char* s, const char* x), that |
Solution
Here is the program and please let me know if any errors occurs
char* findx (const char* s, const char* x)
{
assert(s);
assert(x);
size_t len_s = m_strlen(s);
size_t len_x = m_strlen(x);
assert(len_s >= len_x);
char* p_to_match = nullptr;
for (size_t i = 0; i < len_s; ++i)
{
if (*(s + i) == *x)
{
p_to_match = const_cast<char*>(s + i);
if (len_x == 1) return p_to_match;
const char* next_s = (s + i + 1);
const char* first_x = (x + 1);
for (size_t j = 0; j < len_x - 1; ++x)
{
if (*(next_s + j) != *(first_x + j)) break;
if (j == len_x - 2) return p_to_match;
}
}
}
return nullptr;
