Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to replace a string by other string in a text file?

Reply
Thread Tools

how to replace a string by other string in a text file?

 
 
Clever Monkey
Guest
Posts: n/a
 
      05-30-2007
Tor Rustad wrote:
> Umesh wrote:
>> Do you have any answer to it? thx.
>>

> Quit that C programming class, you don't have what it takes to become a
> C programmer anyway.
>

Oh, I don't know. It is a truism repeated often enough the coders are lazy.
--
clvrmnky <(E-Mail Removed)>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      05-30-2007
Clever Monkey wrote:
> Tor Rustad wrote:
>> Umesh wrote:
>>> Do you have any answer to it? thx.
>>>

>> Quit that C programming class, you don't have what it takes to become
>> a C programmer anyway.
>>

> Oh, I don't know. It is a truism repeated often enough the coders are
> lazy.


Yeah, repeated by many monkeys that think themselves clever
but...

Why generalizing from this lazy student asking for &a homework
question to coders in general?

Why is it that our profession is so looked down by most people that
even in newsgroups that are meant for coders like this one we
have to bear those stupid remarks?

jacob
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      05-30-2007
Clever Monkey wrote:

> Tor Rustad wrote:
>> Umesh wrote:
>>> Do you have any answer to it? thx.
>>>

>> Quit that C programming class, you don't have what it takes to become a
>> C programmer anyway.
>>

> Oh, I don't know. It is a truism repeated often enough the coders are lazy.


Lazy, but not stupid. (And necessary, but not sufficient.)

"If taking devious pains was a sign of laziness, he was prepared to
call Hazelton the laziest man alive". (_Cities in Flight_, from
memory and so likely not word-for-word.)

--
"Who do you serve, and who do you trust?" /Crusade/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-30-2007
jacob navia said:

> Clever Monkey wrote:
>> Tor Rustad wrote:
>>> Umesh wrote:
>>>> Do you have any answer to it? thx.
>>>>
>>> Quit that C programming class, you don't have what it takes to
>>> become a C programmer anyway.
>>>

>> Oh, I don't know. It is a truism repeated often enough the coders
>> are lazy.

>
> Yeah, repeated by many monkeys that think themselves clever
> but...


You've misunderstood the irony in "Clever Monkey"'s reply.

> Why generalizing from this lazy student asking for &a homework
> question to coders in general?


That's the irony. The thing is simply this: that the OP's laziness is
the *wrong kind*, the kind that seeks other people who are willing to
do one's work. The right kind of laziness is very different, and that's
the kind to which programmers should aspire.

>
> Why is it that our profession is so looked down by most people that
> even in newsgroups that are meant for coders like this one we
> have to bear those stupid remarks?


It isn't, and we don't, and they aren't - in that order.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-31-2007
CBFalconer said:

> Richard Heathfield wrote:
>> jacob navia said:
>>

> ... snip ...
>>
>>> Why is it that our profession is so looked down by most people
>>> that even in newsgroups that are meant for coders like this one
>>> we have to bear those stupid remarks?

>>
>> It isn't, and we don't, and they aren't - in that order.

>
> You omitted a terminal ", we don't".


I don't think so - it's right there, in the middle. Email me if not
entirely satisfied (hardly topical, after all) - see sig for address.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Stan Milam
Guest
Posts: n/a
 
      06-10-2007
Umesh wrote:
> Do you have any answer to it? thx.
>

/************************************************** ********************/
/* File Name: replace.c. */
/* Author: Stan Milam. */
/* Date Written: 13-Jan-2003. */
/* Description: */
/* Implement a function replace all occurances of a pattern. */
/* */
/* (c) Copyright 2007 by Stan Milam. */
/* All rights reserved. */
/* */
/************************************************** ********************/

#include <string.h>
#include <stddef.h>
#include "booldefs.h"

/* $Revision: 1.3 $ */
extern char tb_empty[];
extern char tb_copyright[];
static char *copyright = tb_copyright;

/************************************************** ********************/
/* Name: */
/* replace(). */
/* */
/* Synopsis: */
/* #include "strtools.h" */
/* char *replace(char *target, const char *from, char *to */
/* */
/* Description: */
/* The replace function will replace all instances of the from */
/* pattern with that of the to pattern. If the to pattern is */
/* an empty string or NULL all instances of the from pattern will */
/* be removed from the target string. */
/* */
/* Note: */
/* Since a replacement pattern can be longer the the original */
/* pattern the programmer must allow for enough memory to */
/* accomodate a larger target string to allow successful */
/* completion of the operation. */
/* */
/* Arguments: */
/* char *target - The string in which patterns will be */
/* replaced. Cannot be NULL */
/* */
/* const char *from - The pattern to be replaced. Cannot be NULL */
/* or an empty string (i.e. ""). */
/* */
/* const char *to - The replacement pattern. Using a NULL */
/* pointer will be interpreted the same as an */
/* empty string (i.e. ""). */
/* */
/* Return Value: */
/* In all cases the address of the target string. Should either */
/* the target string or the from pattern be NULL the global errno */
/* variable will be set to EINVAL. */
/* */
/************************************************** ********************/

char *
replace ( char *p_target, const char *p_from, const char *p_to )
{
size_t to_len, from_len;
char *l_to, *ptr, *rv = p_target;

/************************************************** ************/
/* I should not have to do this but some compilers complain */
/* when I try to assign the empty pointer to p_to. p_to */
/* points to a string of const characters. I should be able */
/* to change the pointer! */
/************************************************** ************/

l_to = p_to == NULL ? tb_empty : (char *) p_to;

/************************************************** ************/
/* Get the lengths before we enter the loop. */
/************************************************** ************/

to_len = strlen( l_to );
from_len = strlen( p_from );

for( ptr = strstr(p_target, p_from); ptr; ptr = strstr(ptr, p_from) ) {

/************************************************** ********/
/* We must either shrink out the replaced pattern or make */
/* room for the new pattern. If the two patterns are of */
/* equal length we don't care. */
/************************************************** ********/

if ( to_len != from_len )
memmove(ptr + to_len,ptr + from_len,strlen(ptr + from_len)+1);

/************************************************** ********/
/* Once everthing is adjusted all we have to do is */
/* replace . */
/************************************************** ********/

if ( to_len ) {
memmove( ptr, l_to, to_len );
ptr = ptr + to_len;
}
}

return rv;
}




--
Regards,
Stan Milam
================================================== ===========
Charter Member of The Society for Mediocre Guitar Playing on
Expensive Instruments, Ltd.
================================================== ===========
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-11-2007
On Sun, 10 Jun 2007 19:51:56 GMT, Stan Milam <(E-Mail Removed)>
wrote:

>Umesh wrote:
>> Do you have any answer to it? thx.
>>

>/************************************************** ********************/
>/* File Name: replace.c. */
>/* Author: Stan Milam. */
>/* Date Written: 13-Jan-2003. */
>/* Description: */
>/* Implement a function replace all occurances of a pattern. */
>/* */
>/* (c) Copyright 2007 by Stan Milam. */
>/* All rights reserved. */
>/* */
>/************************************************** ********************/
>
>#include <string.h>
>#include <stddef.h>
>#include "booldefs.h"


What is this?

>
>/* $Revision: 1.3 $ */
>extern char tb_empty[];
>extern char tb_copyright[];


Where are these arrays supposed to be defined?

>static char *copyright = tb_copyright;
>
>/************************************************** ********************/
>/* Name: */
>/* replace(). */
>/* */
>/* Synopsis: */
>/* #include "strtools.h" */
>/* char *replace(char *target, const char *from, char *to */
>/* */
>/* Description: */
>/* The replace function will replace all instances of the from */
>/* pattern with that of the to pattern. If the to pattern is */
>/* an empty string or NULL all instances of the from pattern will */
>/* be removed from the target string. */
>/* */
>/* Note: */
>/* Since a replacement pattern can be longer the the original */
>/* pattern the programmer must allow for enough memory to */
>/* accomodate a larger target string to allow successful */
>/* completion of the operation. */
>/* */
>/* Arguments: */
>/* char *target - The string in which patterns will be */
>/* replaced. Cannot be NULL */
>/* */
>/* const char *from - The pattern to be replaced. Cannot be NULL */
>/* or an empty string (i.e. ""). */
>/* */
>/* const char *to - The replacement pattern. Using a NULL */
>/* pointer will be interpreted the same as an */
>/* empty string (i.e. ""). */
>/* */
>/* Return Value: */
>/* In all cases the address of the target string. Should either */
>/* the target string or the from pattern be NULL the global errno */
>/* variable will be set to EINVAL. */


If you are going to set errno, don't you need to include errno.h?
EINVAL is a non-standard value and the name is reserved for the
implementation.

>/* */
>/************************************************** ********************/
>
>char *
>replace ( char *p_target, const char *p_from, const char *p_to )
>{
> size_t to_len, from_len;
> char *l_to, *ptr, *rv = p_target;
>
> /************************************************** ************/
> /* I should not have to do this but some compilers complain */
> /* when I try to assign the empty pointer to p_to. p_to */
> /* points to a string of const characters. I should be able */
> /* to change the pointer! */
> /************************************************** ************/
>
> l_to = p_to == NULL ? tb_empty : (char *) p_to;


Why do you need l_to at all? If you want p_to to point to tb_empty,
it should be declared const.

>
> /************************************************** ************/
> /* Get the lengths before we enter the loop. */
> /************************************************** ************/
>
> to_len = strlen( l_to );
> from_len = strlen( p_from );
>
> for( ptr = strstr(p_target, p_from); ptr; ptr = strstr(ptr, p_from) ) {
>
> /************************************************** ********/
> /* We must either shrink out the replaced pattern or make */
> /* room for the new pattern. If the two patterns are of */
> /* equal length we don't care. */
> /************************************************** ********/
>
> if ( to_len != from_len )
> memmove(ptr + to_len,ptr + from_len,strlen(ptr + from_len)+1);
>
> /************************************************** ********/
> /* Once everthing is adjusted all we have to do is */
> /* replace . */
> /************************************************** ********/
>
> if ( to_len ) {
> memmove( ptr, l_to, to_len );
> ptr = ptr + to_len;
> }
> }
>
> return rv;


Why do you need rv at all? You can return p_target.

>}


You never set errno as promised in your documentation.


Remove del for email
 
Reply With Quote
 
stmilam@yahoo.com
Guest
Posts: n/a
 
      06-21-2007
On Jun 11, 12:44 am, Barry Schwarz <(E-Mail Removed)> wrote:
> On Sun, 10 Jun 2007 19:51:56 GMT, Stan Milam <(E-Mail Removed)>
> wrote:
>
>
>
>
>
> >Umesh wrote:
> >> Do you have any answer to it? thx.

>
> >/************************************************** ********************/
> >/* File Name: replace.c. */
> >/* Author: Stan Milam. */
> >/* Date Written: 13-Jan-2003. */
> >/* Description: */
> >/* Implement a functionreplaceall occurances of a pattern. */
> >/* */
> >/* (c) Copyright 2007 by Stan Milam. */
> >/* All rights reserved. */
> >/* */
> >/************************************************** ********************/

>
> >#include <string.h>
> >#include <stddef.h>
> >#include "booldefs.h"

>
> What is this?
>
>
>
> >/* $Revision: 1.3 $ */
> >extern char tb_empty[];
> >extern char tb_copyright[];

>
> Where are these arrays supposed to be defined?
>
>
>
>
>
> >static char *copyright = tb_copyright;

>
> >/************************************************** ********************/
> >/* Name: */
> >/* replace(). */
> >/* */
> >/* Synopsis: */
> >/* #include "strtools.h" */
> >/* char *replace(char *target, const char *from, char *to */
> >/* */
> >/* Description: */
> >/* Thereplacefunction willreplaceall instances of the from */
> >/* pattern with that of the to pattern. If the to pattern is */
> >/* an empty string or NULL all instances of the from pattern will */
> >/* be removed from the target string. */
> >/* */
> >/* Note: */
> >/* Since a replacement pattern can be longer the the original */
> >/* pattern the programmer must allow for enough memory to */
> >/* accomodate a larger target string to allow successful */
> >/* completion of the operation. */
> >/* */
> >/* Arguments: */
> >/* char *target - The string in which patterns will be */
> >/* replaced. Cannot be NULL */
> >/* */
> >/* const char *from - The pattern to be replaced. Cannot be NULL */
> >/* or an empty string (i.e. ""). */
> >/* */
> >/* const char *to - The replacement pattern. Using a NULL */
> >/* pointer will be interpreted the same as an */
> >/* empty string (i.e. ""). */
> >/* */
> >/* Return Value: */
> >/* In all cases the address of the target string. Should either */
> >/* the target string or the from pattern be NULL the global errno */
> >/* variable will be set to EINVAL. */

>
> If you are going to set errno, don't you need to include errno.h?
> EINVAL is a non-standard value and the name is reserved for the
> implementation.
>
>
>
>
>
> >/* */
> >/************************************************** ********************/

>
> >char *
> >replace( char *p_target, const char *p_from, const char *p_to )
> >{
> > size_t to_len, from_len;
> > char *l_to, *ptr, *rv = p_target;

>
> > /************************************************** ************/
> > /* I should not have to do this but some compilers complain */
> > /* when I try to assign the empty pointer to p_to. p_to */
> > /* points to a string of const characters. I should be able */
> > /* to change the pointer! */
> > /************************************************** ************/

>
> > l_to = p_to == NULL ? tb_empty : (char *) p_to;

>
> Why do you need l_to at all? If you want p_to to point to tb_empty,
> it should be declared const.
>
>
>
>
>
>
>
> > /************************************************** ************/
> > /* Get the lengths before we enter the loop. */
> > /************************************************** ************/

>
> > to_len = strlen( l_to );
> > from_len = strlen( p_from );

>
> > for( ptr = strstr(p_target, p_from); ptr; ptr = strstr(ptr, p_from) ) {

>
> > /************************************************** ********/
> > /* We must either shrink out the replaced pattern or make */
> > /* room for the new pattern. If the two patterns are of */
> > /* equal length we don't care. */
> > /************************************************** ********/

>
> > if ( to_len != from_len )
> > memmove(ptr + to_len,ptr + from_len,strlen(ptr + from_len)+1);

>
> > /************************************************** ********/
> > /* Once everthing is adjusted all we have to do is */
> > /*replace. */
> > /************************************************** ********/

>
> > if ( to_len ) {
> > memmove( ptr, l_to, to_len );
> > ptr = ptr + to_len;
> > }
> > }

>
> > return rv;

>
> Why do you need rv at all? You can return p_target.
>
> >}

>
> You never set errno as promised in your documentation.
>
> Remove del for email- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -


My bad. I changed the code without changing the documentation. I've
fixed it now. As to the other questions, well, I figured if the OP
has any intelligence he can extrapolate from the example.

Stan.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
replace a string delimited by 2 other string, regexp problem Sébastien Maurette Ruby 3 10-02-2006 09:40 PM
replace text in IFRAME using innerHTML.replace(...) possible? mscir Javascript 0 10-11-2005 11:21 PM
replace instances of text on page with image or other text? juglesh Javascript 16 12-27-2004 04:14 AM



Advertisments