-5

Most of the modules I found online will replace just the first occurrence of that string. But I want a module that will search every occurrence and will replace that string. I found a code in stack overflow which was so very tedious, so I'm not looking into it. And the environment where I'm working, supports only C compiler. So C++ command tweaks comments are not appreciated. This is as far as I've come. It still throws me errors. Any help is appreciated.

#include <stdio.h>
#include <string.h>
char *replace_str(char *str, char *orig, char *rep)
{
  char *buffer;
  char *p, *work;

  while(strstr(str,orig))
  {
  p = strstr(str, orig);  

  strncpy(work, str, p-str);
  strcat(buffer, work);
  //strncpy(buffer+strlen(buffer), str, p-str);
  strcat(buffer, rep);
  p+=strlen(orig);
  str = p;
  }
   return strcat(buffer,p);
}
int main(void)
{
  puts(replace_str("Hello, Kate! I once had a cat named Kate! ", "Kate", "Paul"));
  return 0;
}
Shiva S
  • 33
  • 6
  • *I found a code in stack overflow which was so very tedious, so I'm not looking into it* Not the most constructive approach. If we answer how will we know whether it will be too complicated for you ? – cnicutar Jan 24 '14 at 22:57
  • Show me the "tedious" code first, and if I agree with you on its "tediousness" I will answer your question. – kuroi neko Jan 24 '14 at 22:59
  • @cnicutar: Very true. It's just that it is 5am in the morning, and the simpler the code - the easier the logic. Because this code that I'm making, will have to be edited by someone, someday. And I don't want the same person thinking this way, that the code is too big to debug. Sorry if I was point blank on that one. Just seemed fair to be true. – Shiva S Jan 24 '14 at 22:59
  • You might want to tell us what errors it's producing, and tell us what you've done to try to understand what the errors mean. Folks are unlikely to make much effort to debug your code unless you meet them at least halfway. (Tried a debugger yet? Tried inserting printouts to see how the values are changing?) – keshlam Jan 24 '14 at 23:00
  • @kuroineko: http://stackoverflow.com/a/12546318/1664596. – Shiva S Jan 24 '14 at 23:00
  • @keshlam: A very good point. No, I didn't try a debugger. Working on a mac with no xCode. Codepad.org is my only option. It just kept telling me "Segmentation fault". But a friend's laptop had a C environment which just takes the code and produces no errors when I compile. But when I run it, it produces undesired results. And inserting printf statements are what that got me this far. Sorry, if I'm not making it easier for you. – Shiva S Jan 24 '14 at 23:03
  • 1
    I started to provide an answer to your question, but I found it was *so very tedious*, so I just stopped and submitted this comment instead. – Brandin Jan 24 '14 at 23:07
  • [a working version](http://ideone.com/6Cj4Ee). Too bad the post was closed, you won't have any more explanations than what is in the source. – kuroi neko Jan 25 '14 at 01:54
  • @kuroineko: Thanks, this one follows a different mechanism than the one I'm trying, but very good. Thanks for your input. – Shiva S Jan 25 '14 at 10:27

1 Answers1

2

Your code produces undefined behaviour because you try to copy from/to both buffer and work without allocating memory for them. Use malloc and don't forget to free everything properly again.

If the replacement is constrained to be the same length as the string to be replaced, then you don't need the buffers at all. Otherwise you have to make sure you free the returned string later in the calling part.

  • But using malloc produced me junk values. Like in this code: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=10890&lngWId=3 – Shiva S Jan 24 '14 at 23:07
  • Also that if you read the comments of that post you'll see that one person has commented saying it is not very clever to use malloc since it stores in very undesired places. I'm not sure what exactly he was trying to convey. – Shiva S Jan 24 '14 at 23:08
  • 1
    The comment you are referencing explains why the code in the link should not be used. `malloc(0)` means: allocate zero bytes. That makes no sense. The problem is not that you shouldn't use `malloc`, the problem is that the author of the code in the link did not use it correctly. You have to provide the string length (+1 for terminating zero) to `malloc`. –  Jan 24 '14 at 23:10
  • Thanks, that's really helpful. But what if my string length is changing. Like in this case for buffer. What am I supposed to define then? – Shiva S Jan 24 '14 at 23:34
  • @ShivaS That is the reason why your task isn't that trivial at all. You will have to compute the new size first (before copying anything to the new buffer) or reallocate as necessary. –  Jan 24 '14 at 23:37
  • Very good. Thank you. :) – Shiva S Jan 24 '14 at 23:43