Question
Explain the steps in the following code: Can someone explain what the following code is doing step by step or why these steps are being
Explain the steps in the following code:
Can someone explain what the following code is doing step by step or why these steps are being taken? We just got introduced to C-String so I'm a little confused. Thanks.
#include
string STUDENT = "hsohn2"; // Add your name Blackboard/occ-email ID
#include "h21.h"
const char * findStr (const char *str1, const char *str2) { if (*str2 == '\0') return str1; const char * result = nullptr; const char * beg = str1; while(*beg ) { const char * p1 = str2; const char * p2 = beg; bool found = true; while(*p1 && *p2 ) { if(*p1 != *p2) { found = false; break; } p1++; p2++; } if(found ) return beg; beg++; } return result; }
This is what the function is supposed to do:
/** * Finds the first occurrence of str2 which appears in str1. * Returns a pointer to the first occurrence of str2 in str1. * If no match is found, then a null pointer is returned. * If str2 points to a string of zero length, then the argument str1 is returned. * * @param str1 C-string to search through. * @param str2 C-string to search for. * @return */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started