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");
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.