Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I need to write my own implementation of a function which finds a string (word1) in another string (text) and replaces all instances of word1 in text with a third string (word2). I get the following message on debugging
Quote:
Unhandled exception at 0x5489F791 (msvcr110d.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0191ECBA.


What I have tried:

This is what I have thus far;


C++
void findandreplace(char text[],const char word1[], const char word2[])
    {
            char *start;
            char *end;
            start=strstr(text,word1);
            end=start;
        if (strcmp(text,start))
        {
            end+=strlen(word2);
            strcpy(&text[end-start+1],&text[(int)start]);
            strcpy(text,word2);
            findandreplace(end,word1,word2);
        }
        if (!strcmp(text,start))
	    {
            end++;
            findandreplace(end,word1,word2);
        }
        if (!text)
        {
            return;
        }
    }


I'm sure I have made plenty of mistakes in writing this, but please keep in mind I am essentially a complete noob. Any help pointing out mistakes and possible corrections would be greatly appreciated.
Posted
Updated 17-May-16 12:25pm
v2
Comments
CPallini 16-May-16 15:56pm    
Is recursion mandatory?
Must you use C programming language or C++ standard library usage is allowed?
Member 12527108 16-May-16 15:58pm    
Recursion is not mandatory but preferable.
C++ standard library usage is allowed.
Sergey Alexandrovich Kryukov 16-May-16 16:43pm    
If cannot be preferable in this case just because it is even practically unacceptable.
Anyway, use the debugger. The problem is not so simple as it seems, but if you use standard library, it is already solved.
—SA
jeron1 16-May-16 17:08pm    
Agreed!
jeron1 16-May-16 16:01pm    
Does the code work as intended?

Iterative, C++ version
C++
void find_and_replace( string & text, const string & word1, const string & word2)
{
  size_t pos = string::npos;
  size_t len = word1.length();

  while ( (pos = text.rfind(word1, pos)) != string::npos )
    text.replace( pos, len, word2);
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-May-16 18:42pm    
5ed. One of the cases when recursion, which could work, in principle, on small data sets, is not just unsuitable, but ridiculously unsuitable. :-)
—SA
CPallini 17-May-16 3:14am    
Thank you.
Don't use recursion in this case, this function is suited for looping.
since you are using char and I don't see C++ syntax, you can as well Google for
C string replace

The first link in Googke: What is the function to replace string in C? - Stack Overflow[^] contain this code as first answer.
C++
// You must free the result if result is non-NULL.
char *str_replace(char *orig, char *rep, char *with) {
    char *result; // the return string
    char *ins;    // the next insert point
    char *tmp;    // varies
    int len_rep;  // length of rep
    int len_with; // length of with
    int len_front; // distance between rep and end of last rep
    int count;    // number of replacements

    if (!orig)
        return NULL;
    if (!rep)
        rep = "";
    len_rep = strlen(rep);
    if (!with)
        with = "";
    len_with = strlen(with);

    ins = orig;
    for (count = 0; tmp = strstr(ins, rep); ++count) {
        ins = tmp + len_rep;
    }

    // first time through the loop, all the variable are set correctly
    // from here on,
    //    tmp points to the end of the result string
    //    ins points to the next occurrence of rep in orig
    //    orig points to the remainder of orig after "end of rep"
    tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);

    if (!result)
        return NULL;

    while (count--) {
        ins = strstr(orig, rep);
        len_front = ins - orig;
        tmp = strncpy(tmp, orig, len_front) + len_front;
        tmp = strcpy(tmp, with) + len_with;
        orig += len_front + len_rep; // move to next "end of rep"
    }
    strcpy(tmp, orig);
    return result;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900