Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Explain the steps in the following code: Can someone explain what the following

ID: 3604963 • Letter: E

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 taken? We just got introduced to C-String so I'm a little confused. Thanks.

#include <string>
// Add additional headers here
using namespace std;

string STUDENT = "hsohn2"; // Add your name Blackboard/occ-email ID

#include "h21.h"

const char * findStr (const char *str1, const char *str2)
{
if (*str2 == '') 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
*/

Explanation / Answer

/*Reference code is given below:*/

const char * findStr (const char *str1, const char *str2)
{
if (*str2 == '') 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;
}

Explanation:

This function takes two string as arguments, 1st one is the source string and 2nd one is the string which we would search for in the source string. If 2nd string is present in the source string then it returns the pointer to the first occurrence of the 2nd string within the source string. If the 2nd string is a string with 0 length i.e no character is there, then the function would return the whole source string as a result, if 2nd string is not present in the source string then null pointer is returned.

Now, first take this line: if (*str2 == '') return str1-------- > this line checks whether then 2nd string is of length 0; if it is of 0 length then the source string i.e. str1 is returned and function ends; if this condition is not satisfied then next part of the function would execute. This condition is not satisfying means the length of the 2nd string is not 0.

Next, take this two lines: const char * result = nullptr;const char * beg = str1;

These two line declare and initializes two character pointers, one character pointer named result is initializes with null; another character pointer named beg is initializes with str1, i.e. with source string;it would help to iterate over the strings for searching purpose.

Next an outer while loop starts and this loop will continue until beg pointer points to null, i.e. until the source string is totally traversed.

Within this loop, again two pointers are defined as const char * p1 = str2;const char * p2 = beg;p1 would points to 2nd string str2 and p2 would points to the beg pointer, for 1st iteration p2 and beg both points to beginning to the source string.

Now bool found = true; defines a Boolean variable and initializes with true value.

Next inner while loop starts and continues until p1 and p2 both exists,i.e. if any of them is null then this loop ends;

Within this inner loop character by character strings are compared with p1 and p2 pointer; first p1 points to the string’s which is searched for, and p2 points to the beg; if character by character matches then p1 and p2 increases to point next character; whenever this comparison fails then the loop ends and found assigns to false; found variable true means the search string is found within the source string and the beginning of the search string within the source string is denoted by beg pointer;

After ending the inner while loop the line------- > if(found ) return beg; checks whether found variable is true or not, if it is true that means the search string is found in the source string and the starting position would be given by beg pointer, so the beg is returned from the function and execution of this function is ended. But it found is not true, then the search string is not found within the source string’s part: from the beg position to the length of the search string; then we have to increase the starting position of search within the source string and that can be done by increasing the position of beg pointer by 1 i.e. beg++;------ >by this line. For example, the outer while loop starts when beg points to the starting character of source string; in next iteration the beg would point to the 2nd character of the source string and matching process would starts from the 2nd character of the source string in 2nd iteration. And thus process would continue until the search string is found;

After the increasing of beg pointer the outer while loop’s next iteration starts and it continues until this beg pointer points to null; if str2 string is not found in str1 then only this loop ends and after that result pointer is returned as it contains null; if str2 string is found in str1 then within the while loop the return statement is executed.

Thus this function works.

/*Hope this explanation would help you. Thank you!!*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote