I'm trying to write a dynamic function to replace any substring within a string with a new substring.

0 Benjamin Soung · September 4, 2015
Hey guys.

I'm sort of lost on what to do. I created a separate function that counts the total characters of the entire string, and tried putting it into my main function, but there are some errors. that pop out. Basically, I need to use this string below:

"Mr. Fay, is this going to be a battle of wits?" 
"If it is," was the indifferent retort, "you have come unarmed!"

And I have to write a function that will replace any word above with a new word of any choosing. 

When I try to run my code, I get an error on 'else if((str[i+e]==data[j+e]) && (j<n))' with the message 'Thread 1: EXC_BAD_INSTRUCTION (code=12, subcode=0x0)

Here's what I have so far:

#include <stdio.h>
#include <string.h>

int dynamic();

int main()
{
    
    
    char str[]="\n\"Mr. Fay, is this going to be a battle of wits?\" \n\"If it is,\" was the indifferent retort, \"you have come unarmed!\"";
    
    int i,j = 0,k = 0,l = 0, n = 0;
    
    unsigned int e = n - 2;
    
    char data[150];
    
    char newData[150];
    
    char newStr[150];
    
    printf("Give me a substring from the string");
    gets(data);
    
    printf("Give me a substring to replace it with");
    gets(newData);
    
    dynamic();
    
    for(i=0;str!='\0';i++)
    {
        
        if(str!=data[j])
        {
            newStr[l]=str;
            l++;
        }
        else if((str[i+e]==data[j+e]) && (j<n))
        {
            newStr[l]=newData[j];
            j++;
            l++;
            e--;
            
        }
        else if((str[i+e]==data[j+e]) && (j>=n))
        {
            j++;
            e--;
        }
        else
        {
            newStr[l]=str;
            l++;
        }
        
    }
    
    
    printf("original string is-");
    
    for(k=0;k<n;k++)
        printf("%c",str[k]);
    printf("\n");
    
    printf("modified string is-");
    
    for(k=0;k<n;k++)
        printf("%c",newStr[k]);
    
    printf("\n");
}

int dynamic()
{
    char str[]="\n\"Mr. Fay, is this going to be a battle of wits?\" \n\"If it is,\" was the indifferent retort, \"you have come unarmed!\"";
    
    int i,n=0;
    
    for(i=0;str!='\0';i++)
    {
        n++;
    }
    printf("the number of characters is %d\n",n);
    
    return (n);
    
}


Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · September 5, 2015
Honestly, this problem is a joke in C++, but if you insist on doing it in C, here is my recommendation.

I am not going to write the code out, but here is my suggestion.

Break this problem down. Here is a relatively safe procedure to replace the substring.

1) Write a function to identify a substring. Note where the substring begins and ends.
2) Count the number of characters up to the start of the substring and store it in a variable.
3) Count the number of characters within the replacement substring.
4) Count the number of characters after the substring ends.
5) Allocate a new char array of the size of the part before the substring, the replacement string, and the part after the substring.
6) Use memcpy to copy the part before the substring into the new array.
7) Then use memcpy to copy the replacement string into the new array.
8) Use memcpy to copy the part after the substring into the array.
9) Print out the new array. You are done.
  • 1

C

107,024 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator