LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 108Reputation: 108

Ciao,

Code:
char *stringa_sost(char *buffer3, char *buffer1, char *buffer2)
is a definition. And when you call it
Code:
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
ozmosis
LQ Newbie
 
Registered: Apr 2007
Posts: 9

Rep: Reputation: 0
delete!!!!

Last edited by ozmosis; 05-03-2007 at 10:28 AM.
 
Old 04-25-2007, 08:31 AM   #18
reverse
Member
 
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.

#1.
Code:
  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:

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

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

DESCRIPTION
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:

Code:
  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*/
Yet:

Code:
  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:

Code:
  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:

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

Code:
(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
ozmosis
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
paulhope123
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));
}
else
{
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
dhondi
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

Quote:
Originally Posted by itsme86 View Post
Code:
#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!
itsme@dreams:~/C$
 
Old 11-09-2009, 09:59 PM   #22
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

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


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 PM.

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