Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-24-2007, 04:42 PM   #16
Senior Member
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie stretch buster
Posts: 1,643

Rep: Reputation: 98


char *stringa_sost(char *buffer3, char *buffer1, char *buffer2)
is a definition. And when you call it
stringa_sost(char buffer3[1000][1000], char buffer1[256], char buffer2[]);
INCONSISTANT!! You do not need type identifier when you call function.

I do not know anything about your compiler, dose it work?

Last edited by kaz2100; 04-24-2007 at 04:45 PM.
Old 04-25-2007, 06:21 AM   #17
LQ Newbie
Registered: Apr 2007
Posts: 9

Rep: Reputation: 0

Last edited by ozmosis; 05-03-2007 at 10:28 AM.
Old 04-25-2007, 08:31 AM   #18
Registered: Apr 2007
Distribution: Gentoo
Posts: 337

Rep: Reputation: 30
There are a couple of problems with your code, so I'll try to say some things about some of them.

  strncpy(buffer5, buffer3, p-buffer3); /*???*/
  buffer5[p-buffer3] = '\0';
Looking at that comment it would seem you have no idea what the two instructions do. Do you understand what strncpy() does? I'll paste an excerpt from the manual page of strcpy, on my Gentoo Linux system:

char *strcpy(char *dest, const char *src);

char *strncpy(char *dest, const char *src, size_t n);

The strcpy() function copies the string pointed to by src (including the terminat-
ing `\0' character) to the array pointed to by dest. The strings may not overlap,
and the destination string dest must be large enough to receive the copy.

The strncpy() function is similar, except that not more than n bytes of src are
copied. Thus, if there is no null byte among the first n bytes of src, the result
will not be null-terminated.
#2. Another thing:

  char buffer1 [256];	/*prima stringa da acquisire da cercare*/
  char buffer2 [256];	/*seconda stringa da sostituire alla prima stringa trovata*/
  char buffer3 [1000];	/*per contenere il testo nel file1*/

  char buffer5[4096];
buffer5 seems to be too big. Why not allocate it dynamically? If you don't want to do this but decide to fine tune the size to a lower value .. make sure it's not so low as to allow buffer overflows to occur.

#3. I think this is what kaz2100 meant.. Your function returns a pointer to char. However you do nothing with this return value:

  stringa_sost(buffer3, buffer1, buffer2);
^^ That is the actual call to the function. As you can see, stringa_sost() could be of type void for all main() cares. I'm thinking: you probably think that by calling:

stringa_sost(string, substring-to-replace, string-to-replace-with);
"string" will be modified. In fact it isn't.

(kind of pseudo-code:)

string = "abcXXXdef";
substring-to-replace = "XXX";
string-to-replace-with = "YYY";

stringa_sost(string, substring-to-replace, string-to-replace-with);

-- At this point, you think: string = "abcYYYdef". In fact, it isn't. string = "abcXXXdef".

However: newstring = stringa_sost(string, substring-to-replace, string-to-replace-with);
Then: newstring = "abcYYYdef".
#4. You should develop the habit of checking the return values of functions. I.e. fopen(), fgets() etc.

Last edited by reverse; 04-25-2007 at 08:35 AM.
Old 04-25-2007, 04:16 PM   #19
LQ Newbie
Registered: Apr 2007
Posts: 9

Rep: Reputation: 0
Thanks a lot for your interest.

Last edited by ozmosis; 05-03-2007 at 10:28 AM.
Old 07-07-2009, 02:33 PM   #20
LQ Newbie
Registered: Jul 2009
Posts: 1

Rep: Reputation: 0
Possible Flaw and Fix

Hi All,

The problem that I found with the original code was that whenever you attempt to replace a substring that's shorter in lenght that the new string, the buffer overflows and the logic crashes. I have added additional logic to fix this problem should you ever encounter such a problem.

char *replace(char *st, char *orig, char *repl)
static char buffer[4096];
static char buffer2[4096];

char *ch;

if (!(ch = (char*)strstr(st, orig)))
return (char*)st;

strncpy(buffer, st, ch-st);
strncpy(buffer2, st, ch-st+strlen(orig));

buffer[ch-st] = '\0';
buffer2[ch-st+strlen(orig)] = '\0';

if (strlen(orig)>=strlen(repl))
sprintf(buffer+(ch-st), "%s%s", repl, ch+strlen(orig));
sprintf(buffer2+(ch-st), "%s%s", repl, ch+strlen(orig));
strcpy(buffer, buffer2, strlen(buffer));

return buffer;
Old 11-09-2009, 08:16 AM   #21
LQ Newbie
Registered: Nov 2009
Posts: 1

Rep: Reputation: 0
Thumbs up dhondi srikanth: replace a substring with another string in C Reply to Thread

Thanks, well done

Originally Posted by itsme86 View Post
#include <stdio.h>
#include <string.h>

char *replace_str(char *str, char *orig, char *rep)
  static char buffer[4096];
  char *p;

  if(!(p = strstr(str, orig)))  // Is 'orig' even in 'str'?
    return str;

  strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
  buffer[p-str] = '\0';

  sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

  return buffer;

int main(void)
  puts(replace_str("Hello, world!", "world", "Miami"));

  return 0;
itsme@dreams:~/C$ ./repstr
Hello, Miami!
Old 11-09-2009, 09:59 PM   #22
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint Debian Edition (LMDE)
Posts: 215

Rep: Reputation: 31
Originally Posted by ozmosis View Post
Dude, you deleted 6 posts on this forum! What's going on???


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace string Johng Programming 9 01-13-2010 04:50 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
Replace substring with SED marri Programming 2 07-09-2005 05:18 PM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration