Question: My C program is coming up with a memory allocation. I was specifically told my my instructor to rethink how I allocate memory in substrinToEnd.
My C program is coming up with a memory allocation. I was specifically told my my instructor to "rethink how I allocate memory in substrinToEnd." I don't know what's wrong with is. I will include my whole file plus what the header file looks like
#include
int strContains(const char *str, const char *subStr){ int sLength = strlen(str); int subLength = strlen(subStr);
if(subLength > sLength){ return 0; } for(int i = 0; i < sLength; i++){ int j = 0; int k = i; //only goes if they match while(str[k] == subStr[j] && j < subLength){ k++; j++; } if(j == subLength){ return 1; } } return 0; }
char *concatenate(const char *s, const char *t){ //get the length of strings int sLength = strlen(s); int tLength = strlen(t);
char* newStr = (char *)malloc(sLength + tLength); int i = 0; //copy s for(i = 0; i < sLength; i++){ newStr[i] = s[i]; } //copy t for(int j = 0; j < tLength; j++){ newStr[i++] = t[j]; } //return new string newStr[i++] = '\0'; return newStr; }
char *append(const char *s, const char *t){ int sLength = strlen(s); int tLength = strlen(t);
//make new strings char* newStr = (char *)malloc(sLength + tLength); int i = 0;
for(i = 0; i < sLength; i++){ newStr[i] = s[i]; } for(int j = 0; j < tLength; j++){ newStr[i++] = t[j]; } newStr[i++] = '\0';
//assigns newStr to s s = newStr; return newStr; }
char *prepend(const char *s, const char *t){ //prepend s and t is equal to prepend t and s return append(t, s); }
char *substringToEnd(const char *s, int beginningIndex){ int stringLen = strlen(s); int subStringLen = stringLen - beginningIndex; char *str;
if(subStringLen < 0){ // str = (char *)malloc(sizeof(char) * 1); // str[1] = '\0'; // return str; return NULL; } else{ str = (char *)malloc((subStringLen) * sizeof(char)); int i, count = 0;
for(i = beginningIndex; i < stringLen; i++){ str[count] = s[i]; count++; } str[count] = '\0'; } return str; }
char *substringIndex(const char *s, int beginIndex, int endIndex){ int stringLen = strlen(s); int subStringLength = stringLen - beginIndex; char *str; if(subStringLength < 0){ str = (char *)malloc(1 * sizeof(char)); str[1] = '\0'; return str; } else if(endIndex < beginIndex){ //swap int temp = endIndex; endIndex = beginIndex; beginIndex = temp;
}
str = (char *)malloc(1 * sizeof(char)); int j, count = 0;
for(j = beginIndex; j < stringLen; j++){ str[count] = s[j]; count++; } str[count] = '\0';
return str; }
char *substringSize(const char *s, int beginningIndex, int size){ int stringLen = strlen(s); int subStringLength = stringLen - beginningIndex; int endIndex = beginningIndex + size; char *str;
if(subStringLength < 0 || endIndex < beginningIndex){ str = (char *)malloc(1 * sizeof(char)); str[1] = '\0'; return str; } else{ str = (char *)malloc(1 * sizeof(char)); int h, count = 0;
for(h = beginningIndex; h < stringLen && h < endIndex; h++){ str[count] = s[h]; count++; } str[count] = '\0'; } return str; }
Header File:
//determines if given string contains a substring int strContains(const char *str, const char *subStr);
//takes two strings and concatenates then together into a new string char *concatenate(const char *s, const char *t);
//takes two strings (s and t) and appends t to s; resulting in a new string char *append(const char *s, const char *t);
//takes two strings (s and t) and prepends t to s; resulting in a new string char *prepend(const char *s, const char *t);
//returns a new copy of a substring from index to end char *substringToEnd(const char *s, int beginningIndex);
//returns a new copy of a substring from an inclusive index to specified end char *substringIndex(const char *s, int beginIndex, int endIndex);
//returns a new copy of a substring from an index of a particular size char *substringSize(const char *s, int beginningIndex, int size);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
