Ive been creating my own extended string library one of the functions is a str_replace function.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *str_replace(char *find, char *replacement, const char *string);
int main()
{
char string[] = "Testing String Testing String String test string";
char *newString = str_replace("Testing","test",string);
char *newString2 = str_replace("string", "", newString);
printf("String: \t\t%s\nNew String: \t\t%s\nNew String 2: \t\t%s\n", string, newString, newString2);
free(newString);
free(newString2);
return 0;
}
char *str_replace(char *find, char *replacement, const char *string)
{
char *ptr, *newString, *compString, *restOfString;
int findLength = 0, position = 0, endPosition = 0;
int i; /* for loop */
/* String Lengths and Difference */
findLength = strlen(find);
/* Allocate Memory for compString */
if ((compString = malloc(strlen(string) + 1))==NULL)
{
fprintf(stderr, "Memory Allocation Error");
exit(1);
}
strcpy(compString,string);
/* Allocate 1 char for null character for newString (used to return the new string */
if ((newString = malloc(1))==NULL)
{
fprintf(stderr, "Memory Allocation Error");
exit(1);
}
/* Find and Replace all Strings */
while (compString != NULL) /* Set to Null incase we find a substring on the end of the string */
{
/* Find Substr */
if ((ptr=strstr(compString, find))!=NULL)
position=ptr-compString;
else
{
/* Why ?
Cos there is possibly more on the string we need to return the entire string with the find strings replaced
*/
if ((newString = realloc(newString, strlen(newString) + strlen(compString) + 1))==NULL)
{
fprintf(stderr, "Memory Allocation Error");
exit(1);
}
/* Concatenate the end of the string */
strcat(newString, compString);
/* Free compString */
free(compString);
return newString; /* All Possible strings to be found have been we need to kill this loop */
}
if (position != 0)
{
if ((restOfString=malloc(position))==NULL)
{
fprintf(stderr, "Memory Allocation Error");
exit(1);
}
/* Copy the String to restOfString before finding the substr */
for (i=0;i<=position;i++)
(restOfString)[i]=(compString)[i];
(restOfString)[position]='\0';
/* Allocate Memeory for restOfString for newString */
if ((newString = realloc(newString, strlen(newString) + strlen(restOfString) + strlen(replacement) + 1))==NULL)
{
fprintf(stderr, "Memory Allocation Error");
exit(1);
}
/* Concatenate restOfString onto newString */
strcat(newString, restOfString);
free(restOfString);
}
/* Reallocate Memory to newString */
if ((newString = realloc(newString, strlen(newString) + strlen(replacement) + 1))==NULL)
{
fprintf(stderr, "Memory Allocation Error");
exit(1);
}
/* Concatenate new String */
strcat(newString, replacement);
/* Delete from found onwards */
endPosition = position + findLength;
(compString) = (ptr+strlen(find));
}
free(compString);
return newString;
}
The first use of this function on the same string works correctly, however use the same string and function again and I get a seg fault error. Now it could be that im freeing wrong things or something and I simply cannot find where it is.