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

can anyone help me fix this? just fill out whats needed my code is at the bottom

ID: 3604717 • Letter: C

Question

can anyone help me fix this? just fill out whats needed my code is at the bottom

my h28.cpp --------------

#include <string>
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <cstring>
// Other headers if necessary
using namespace std;

#include "h28.h"

// Add your code here
size_t getLen(const char * s)
{
const char *str = s;
char * result;
while(*str) str++;
size_t len = str - s;
return len;
}
const char * findStr(const char *str1, const char *str2)
{
char result[255] = "A";
size_t len = getLen(str1);
size_t len2 = getLen(str2);

int j = 0;
for(int i = 0; i < len; i++)
{
if(strncmp(str1,str2,len2) == 0)
{
return str1;
}
}
return NULL;
}

---------------

my h28.h-----------

/**
@file h28.h
@author Stephen Gilbert
@version Declarations for CS 150 Homework
*/
#ifndef H28_H_
#define H28_H_

/**
* 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
*/
const char * findStr(const char *str1, const char *str2);

#endif
// DO NOT CHANGE ANY OF THESE LINES
#define strcmp static_assert(false, "strcmp not allowed");
#define strstr static_assert(false, "strstr not allowed");
#define strlen(s) static_assert(false, "strlen not allowed");
#define strcat(d, s) static_assert(false, "strcat not allowed");
#define strcpy(d, s) static_assert(false, "strcpy not allowed");
#define string static_assert(false, "string not allowed");

/workspace/cs150/hw/h28/ $make test h28.cpp:22:12: warning: unused variable 'result' [-Wunused-variable] char * result; h28.cpp:29:10: warning: unused variable 'result' [-Wunused-variable] char result [255] "A". h28.cpp:33 : 9: warning: unused variable ·j. [-Hunused-variable] h28.cpp:34:22: warning: comparison of integers of different signs: 'int' and 'size t' (aka 'unsigned long) [-Wsign-compare] for(int i=0; i len; i++) 4 warnings generated. TESTING H28--soctavio Checking function: Test your findStr function + Find at beginning.->Sote soma some something somantha solly sonny X Returns nullptr when it shouldn't + Find anything->Sote soma some something somantha solly sonny X Returns nullptr when it shouldn't X Returns nullptr when it shouldn't X Returns nullptr when it shouldn't X Returns nullptr when it shouldn't X Returns nullptr when it shouldn't X Returns nullptr when it shouldn't + Not found. Tests passing 3/10 (30%). H28:soctavio:ALL TESTS -- PASS 3/10 (30%).

Explanation / Answer

#include <string>
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include "h28.h"
#include <cstring>
// Other headers if necessary
using namespace std;

// Add your code here
int getLen(const char * s)
{
const char *str = s;
int len = 0;
char * result;
while(*str){
str++;
++len;
}
return len;
}
const char * findStr(const char *str1, const char *str2)
{
int len = getLen(str1);
int len2 = getLen(str2);
int i ,j = 0 , count;
for (i = 0; i < len;)

{
int s = i;
j = 0;
count = 0;
  
while (str1[i] && str2[j] && (str1[i] == str2[j]))

{
++count;
i++;

j++;

}
if (count == len2)

{

return &str1[s];

}

else

i++;

}   
return NULL;
}
==================================================================

I tested by adding main function. You can run and let me know if there is any concern. I will help

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote