Describe the purpose of pointerDemo function. For example, \"its purpose is to a
ID: 3577557 • Letter: D
Question
Describe the purpose of pointerDemo function. For example, "its purpose is to add two integers Write a simple main function to demonstrate the usage of pointerDemo function. Explain how the code works at these line numbers 16 and 18. You should provide clear and char * pointerDemo (char *s1, char *s2) {if(!*s2) return s1; char *pl = s1; while (*p1) {char * start = p1; char *p2 = s2; while (*pl && *P2 && *p1== *p2) {p1++; p2++;} if(! *p2) return start; p1 = ++start;} return NULL;}Explanation / Answer
a)pointerDemo function finds the first occurance of s2 in s1.
b)
#include<bits/stdc++.h>
using namespace std;
char* pointerDemo(char* s1,char* s2)
{
if(!*s2) return s1;
char* p1=s1;
while(*p1)
{
char* start=p1;
char* p2=s2;
while(*p1&&*p2&&*p1==*p2)
{
p1++;
p2++;
}
if(!*p2) return start;
p1= ++start;
}
return NULL;
}
int main(int argc, char const *argv[])
{
char s1[]="PassingPoint";
char s2[]="Point";
char* s3=pointerDemo(s1,s2);
cout<<s3;
return 0;
}
==========================================================================
output:
Point
==========================================================================
c)Line 16 says that if p2 is empty string then return string pointing to start.i.e.means if occurance of s2 is found in s1 then return pointer of position in s1.
Line 18 says if p2(s2)is not found in start i.e p1,then start from next character for matching
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.