Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Newsgroups | comp.lang.c |
|---|---|
| Subject | Re: Replacing NULLS with space (C strings) |
| References | <jh19o6$qnl$1@speranza.aioe.org> |
| From | Anders Wegge Keller <wegge@wegge.dk> |
| Date | 2012-02-09 22:04 +0100 |
| Message-ID | <878vkbbtwi.fsf@huddi.jernurt.dk> (permalink) |
| Organization | SunSITE.dk - Supporting Open source |
peter <pc722@nospam.mail.com.br> writes:
> In fact, I want to remove all NULLS and EOFs (0x1a)
> from a string then replace them all with spaces. The way I do it
> now is by using a for() loop:
>
> for(temp=0;temp<=strlen(buffer);temp++)
> {
> if(buffer[temp]== '\0' || buffer[temp]==0x1A)
> {buffer[temp]=' ';}
> }
> Is there a faster / more efficient way of doing this?
strlen(buffer) will return the offset of the first '\0' encountered,
so the code above doesn't make that much sense. Also, it is not very
effecient to call strlen for each iteration of the loop. Especially
with patological code like this, the comåpiler will be unable to
optimize the repeated calls away, as you are modifying the object you
are giving as argument.
Either call strlen once and use that result in the entire loop:
len = strlen (buffer);
for (temp = 0 ; temp < len ; temp++) {
if (buffer[temp] == 0x1a) { buffer[temp] = ' '; }
}
Or skip the strlen call entirely, and check for end of string at the
same time as check for modification:
temp = 0;
while (buffer[temp]) {
if (buffer[temp] == 0x1a) { buffer[temp] = ' '; }
temp++;
}
--
/Wegge
Leder efter redundant peering af dk.*,linux.debian.*
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Replacing NULLS with space (C strings) peter <pc722@nospam.mail.com.br> - 2012-02-09 20:19 +0000
Re: Replacing NULLS with space (C strings) James Kuyper <jameskuyper@verizon.net> - 2012-02-09 15:46 -0500
Re: Replacing NULLS with space (C strings) Anders Wegge Keller <wegge@wegge.dk> - 2012-02-09 22:04 +0100
Re: Replacing NULLS with space (C strings) James Kuyper <jameskuyper@verizon.net> - 2012-02-09 16:15 -0500
Re: Replacing NULLS with space (C strings) John Gordon <gordon@panix.com> - 2012-02-09 21:16 +0000
Re: Replacing NULLS with space (C strings) Keith Thompson <kst-u@mib.org> - 2012-02-09 13:29 -0800
Re: Replacing NULLS with space (C strings) Keith Thompson <kst-u@mib.org> - 2012-02-09 13:29 -0800
Re: Replacing NULLS with space (C strings) pete <pfiland@mindspring.com> - 2012-02-09 19:22 -0500
Re: Replacing NULLS with space (C strings) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-09 16:36 -0800
Re: Replacing NULLS with space (C strings) Keith Thompson <kst-u@mib.org> - 2012-02-09 17:27 -0800
Re: Replacing NULLS with space (C strings) Kaz Kylheku <kaz@kylheku.com> - 2012-02-10 02:05 +0000
Re: Replacing NULLS with space (C strings) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-10 05:43 -0800
Re: Replacing NULLS with space (C strings) Shao Miller <sha0.miller@gmail.com> - 2012-02-10 00:27 -0500
Re: Replacing NULLS with space (C strings) Kenneth Brody <kenbrody@spamcop.net> - 2012-02-10 11:42 -0500
Re: Replacing NULLS with space (C strings) John Gordon <gordon@panix.com> - 2012-02-10 16:51 +0000
Re: Replacing NULLS with space (C strings) "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-02-14 14:48 -0800
Re: Replacing NULLS with space (C strings) Keith Thompson <kst-u@mib.org> - 2012-02-09 13:22 -0800
Re: Replacing NULLS with space (C strings) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-09 14:41 -0800
Re: Replacing NULLS with space (C strings) Kenneth Brody <kenbrody@spamcop.net> - 2012-02-10 11:48 -0500
Re: Replacing NULLS with space (C strings) Keith Thompson <kst-u@mib.org> - 2012-02-10 11:08 -0800
Re: Replacing NULLS with space (C strings) Shao Miller <sha0.miller@gmail.com> - 2012-02-10 22:36 -0500
csiph-web