Hello, I am teaching myself programming (not homework) and I have run into the following problem. Quick note before I post the code: I have tried all three of the functions called by the replaceString function and they all work separately. However when I try to use them all (findString, removeString, insertString) in the replaceString function I end up with a "segmentation error. Core dumped."
I have been at this for a while and would love some help. Thanks.. here is the code with the problem written on the top in a comment.
Code:
/* Using the findString, removeString, and insertString functions from preced-
ing exercises, write a function called replaceString that takes three character
string arguments as follows
replaceString (source, s1, s2);
and that replaces s1 inside source with the character string s2.The function
should call the findString function to locate s1 inside source, then call the
removeString function to remove s1 from source, and finally call the
insertString function to insert s2 into source at the proper location.
So, the function call
replaceString (text, "1", "one");
replaces the first occurrence of the character string "1" inside the character string
text, if it exists, with the string "one". Similarly, the function call
replaceString (text, "*", "");
has the effect of removing the first asterisk inside the text array because the
replacement string is the null string.
*/
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
void replaceString (char source[],char s1[], char s2[]);
replaceString("one time let this work", "1", "one");
}
void replaceString (char source[],char s1[], char s2[])
{
int findString (const char source[], const char s[]);
void removeString (char source[], int i, int remove);
void insertString (char source[], char insert[], int p);
int i = findString(source, s2);
int count;
while (s2[count] != '\0')
count++;
removeString(source, i, count);
insertString(source, s1, i);
printf("%s\n", source);
}
int findString (const char source[], const char s[])
{
int i, j, foundit = false;
for ( i = 0; source[i] != '\0' && !foundit; ++i ) {
foundit = true;
for ( j = 0; s[j] != '\0' && foundit; ++j )
if ( source[j + i] != s[j] || source[j + i] == '\0' )
foundit = false;
if (foundit)
return i;
}
return -1;
}
void removeString (char source[], int i, int remove)
// "i" is where to start removing (from findString) and "remove" is # of characters
// to remove
{
char result[81];
int j;
int k = 0;
for (j = 0; j < i; j++)
result[j] = source[j];
while (source[j + remove] != '\0')
{
result[j] = source[j + remove];
j++;
}
while (result[k] != '\0')
{
source[k] = result[k];
k++;
}
source[k] = '\0';
}
void insertString (char source[], char insert[], int p)
// p is where to start inserting and insert[] is what to insert
{
int i = 0;
int j = 0;
char temp[81];
// convert source to a temp
while (source[i] != '\0')
{
temp[i] = source[i];
i++;
}
temp[i + 1] = source[i + 1];
//change source to insert p
while (insert[j] != '\0')
{
source[j + p] = insert[j];
j++;
}
// add back characters deleted by insertion by taking from temp and place after insert
while (temp[p] != '\0')
{
source[j + p] = temp[p];
p++;
}
source[j + p] = '\0';
}