Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #16992 > unrolled thread
| Started by | peter <pc722@nospam.mail.com.br> |
|---|---|
| First post | 2012-02-09 20:19 +0000 |
| Last post | 2012-02-10 22:36 -0500 |
| Articles | 20 on this page of 21 — 11 participants |
Back to article view | Back to comp.lang.c
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
Page 1 of 2 [1] 2 Next page →
| From | peter <pc722@nospam.mail.com.br> |
|---|---|
| Date | 2012-02-09 20:19 +0000 |
| Subject | Replacing NULLS with space (C strings) |
| Message-ID | <jh19o6$qnl$1@speranza.aioe.org> |
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?
[toc] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-02-09 15:46 -0500 |
| Message-ID | <4F3430A4.6000504@verizon.net> |
| In reply to | #16992 |
On 02/09/2012 03:19 PM, peter wrote:
> In fact, I want to remove all NULLS and EOFs (0x1a)
EOF is a macro defined in <stdio.h>. It's required to have a negative
value, which 0x1A does not, so they can't be the same. EOF very
commonly, though not universally, has a value of -1.
There have been systems where 0x1A was used to indicate the end of a
file. However, such systems are far from universal. I'd recommend making
sure that this value is indeed being used that way in all of the
contexts in which you want to use this code.
> 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?
By definition, strlen(buffer) gives you the offset of the very first
null character in buffer (or, if there is none, it keeps searching past
the end of buffer until if finds one; this often results in memory
access violations - make VERY sure that your buffer is in fact null
terminated before calling strlen). Therefore, there's no point in
checking for null characters before you reach then end of the loop; and
you're guaranteed to find one once you reach that end.
I suspect that you have some kind of misunderstanding, that led you to
think that your code could find a null character in some other locations
as well. However, for the rest of this message I'll assume you intended
it to handle null characters exactly the way it actually does.
You have strlen() scanning sequentially through buffer looking for the
first null character, and then you have your for loop scanning
sequentially through buffer looking for null characters and 0x1A. Why
not do it in a single pass?
Your code sets the final terminating null character to blank. This
guarantees that strlen(buffer) can no longer be used to tell you where
that character used to be. If you're planning to do anything further
with that portion of buffer, you'd better do something to keep track of
where it ends.
You don't say what the element type of buffer is; I'll assume it's char;
make appropriate adjustments below if it's something else.
for(char *p = buffer; *p; p++)
if(*p == 0x1A)
*p = ' ';
*p++ = ' ';
ptrdiff_t length = p - buffer;
[toc] | [prev] | [next] | [standalone]
| From | Anders Wegge Keller <wegge@wegge.dk> |
|---|---|
| Date | 2012-02-09 22:04 +0100 |
| Message-ID | <878vkbbtwi.fsf@huddi.jernurt.dk> |
| In reply to | #16992 |
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.*
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-02-09 16:15 -0500 |
| Message-ID | <4F343784.8010007@verizon.net> |
| In reply to | #16996 |
On 02/09/2012 04:04 PM, Anders Wegge Keller wrote:
> 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.
I didn't notice that - that's embarrassing (not as embarrassing as
having written such code, but close). It's worse than merely being
horrendously inefficient; with the terminating null character being
replaced with ' ' inside the loop, followed by immediate recalculation
of the length of the supposedly null-terminated string, the loop will
never terminate until something goes very badly wrong (and possibly not
even then).
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2012-02-09 21:16 +0000 |
| Message-ID | <jh1d2u$8ku$1@reader1.panix.com> |
| In reply to | #16992 |
In <jh19o6$qnl$1@speranza.aioe.org> 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]=' ';}
> }
C strings are terminated by a NULL character. Therefore, by definition,
you won't find any NULLs in the string itself.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-02-09 13:29 -0800 |
| Message-ID | <lnsjijlmqg.fsf@nuthaus.mib.org> |
| In reply to | #16999 |
John Gordon <gordon@panix.com> writes:
> In <jh19o6$qnl$1@speranza.aioe.org> 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]=' ';}
>> }
>
> C strings are terminated by a NULL character. Therefore, by definition,
> you won't find any NULLs in the string itself.
Null is (a macro that expands to) a null *pointer* constant.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-02-09 13:29 -0800 |
| Message-ID | <lnobt7lmpa.fsf@nuthaus.mib.org> |
| In reply to | #17001 |
Keith Thompson <kst-u@mib.org> writes:
> John Gordon <gordon@panix.com> writes:
>> In <jh19o6$qnl$1@speranza.aioe.org> 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]=' ';}
>>> }
>>
>> C strings are terminated by a NULL character. Therefore, by definition,
>> you won't find any NULLs in the string itself.
>
> Null is (a macro that expands to) a null *pointer* constant.
I meant to type NULL, of course.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2012-02-09 19:22 -0500 |
| Message-ID | <4F346336.E97@mindspring.com> |
| In reply to | #16999 |
John Gordon wrote:
>
> In <jh19o6$qnl$1@speranza.aioe.org> 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]=' ';}
> > }
>
> C strings are terminated by a NULL character.
> Therefore, by definition,
> you won't find any NULLs in the string itself.
By definition, a string includes a null character.
ISO/IEC 9899:201x Committee Draft — April 12, 2011 N1570
7. Library
7.1 Introduction
7.1.1 Definitions of terms
1 A string is a contiguous sequence of characters
terminated by and including the first null character.
--
pete
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2012-02-09 16:36 -0800 |
| Message-ID | <76f3fbf8-e9d6-402c-a2f0-e3bbad12540e@h6g2000yqk.googlegroups.com> |
| In reply to | #17015 |
On Feb 10, 12:22 am, pete <pfil...@mindspring.com> wrote: > > By definition, a string includes a null character. > > ISO/IEC 9899:201x Committee Draft — April 12, 2011 N1570 > 7. Library > 7.1 Introduction > 7.1.1 Definitions of terms > 1 A string is a contiguous sequence of characters > terminated by and including the first null character. > In ANSI C terminology. That's so that they can use the term "string" in describing library functions without constantly having to specify that it must be nul-terminated. However the strings in your C program may not be nul-terminated.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-02-09 17:27 -0800 |
| Message-ID | <lnk43vlbox.fsf@nuthaus.mib.org> |
| In reply to | #17017 |
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Feb 10, 12:22 am, pete <pfil...@mindspring.com> wrote:
>> By definition, a string includes a null character.
>>
>> ISO/IEC 9899:201x Committee Draft — April 12, 2011 N1570
>> 7. Library
>> 7.1 Introduction
>> 7.1.1 Definitions of terms
>> 1 A string is a contiguous sequence of characters
>> terminated by and including the first null character.
>>
> In ANSI C terminology. That's so that they can use the term "string"
> in describing library functions without constantly having to specify
> that it must be nul-terminated.
> However the strings in your C program may not be nul-terminated.
Then they're not strings, and calling them that will cause confusion.
They might well be some data structure that acts like a string in a
more general sense, but then there should be an unambiguous name for it.
And so far, we have no idea what kind of data structure the OP is
dealing with, other than an array of characters.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Date | 2012-02-10 02:05 +0000 |
| Message-ID | <20120209180017.261@kylheku.com> |
| In reply to | #17020 |
On 2012-02-10, Keith Thompson <kst-u@mib.org> wrote: > Malcolm McLean <malcolm.mclean5@btinternet.com> writes: >> However the strings in your C program may not be nul-terminated. > > Then they're not strings, and calling them that will cause confusion. No, it won't. If I have a "struct string" in my C program, nobody in their right mind assumes that this still refers to a null-terminated array of char, without looking inside the struct and the surrounding functions. What are you going to complain about next? That we should not use the term "header" in packet processing code because that refers to the units processed by the #include directive? > They might well be some data structure that acts like a string in a > more general sense, but then there should be an unambiguous name for it. That unambiguous name is "character string". You do not get to rename the fundamental concepts in computer science, sorry.
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2012-02-10 05:43 -0800 |
| Message-ID | <8184e1e0-ea9e-41e5-a3e9-7439dc23269a@do4g2000vbb.googlegroups.com> |
| In reply to | #17022 |
On Feb 10, 2:05 am, Kaz Kylheku <k...@kylheku.com> wrote: > On 2012-02-10, Keith Thompson <ks...@mib.org> wrote: > > > Malcolm McLean <malcolm.mcle...@btinternet.com> writes: > >> However the strings in your C program may not be nul-terminated. > > > Then they're not strings, and calling them that will cause confusion. > > No, it won't. If I have a "struct string" in my C program, nobody in their > right mind assumes that this still refers to a null-terminated array of char, > without looking inside the struct and the surrounding functions. > It does cause confusion, of course. Because if you create a struct string then you've got two string types in the program. But calling it struct text or something similar would cause even more confusion. Most people expect a struct string to consist of a character buffer, length member, and maybe a few oddments to indicate a read-only string or a non-ASCII alphabet. I would always nul-terminate the buffer if I could, but not everyone agrees. If for some reason you need strings that index into each other, this might not be possible. -- Malcom's website. Check out the MiniBasic project http://www.malcolmmclean.site11.com/www
[toc] | [prev] | [next] | [standalone]
| From | Shao Miller <sha0.miller@gmail.com> |
|---|---|
| Date | 2012-02-10 00:27 -0500 |
| Message-ID | <jh29nd$ed2$3@dont-email.me> |
| In reply to | #17017 |
On 2/9/2012 19:36, Malcolm McLean wrote: > However the strings in your C program may not be nul-terminated. "peter" versus "pete," for what it's worth.
[toc] | [prev] | [next] | [standalone]
| From | Kenneth Brody <kenbrody@spamcop.net> |
|---|---|
| Date | 2012-02-10 11:42 -0500 |
| Message-ID | <qNmdnSCgb8WN1KjSnZ2dnUVZ_oGdnZ2d@bestweb.net> |
| In reply to | #16999 |
On 2/9/2012 4:16 PM, John Gordon wrote: > In<jh19o6$qnl$1@speranza.aioe.org> peter<pc722@nospam.mail.com.br> writes: > >> In fact, I want to remove all NULLS and EOFs (0x1a) [...] > C strings are terminated by a NULL character. Therefore, by definition, > you won't find any NULLs in the string itself. <nit type="not so minor"> There is no such thing as a "NULL character" in C. Rather, strings are terminated by a "null character". The all-caps "NULL" is a macro representing the "null pointer". </nit> -- Kenneth Brody
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2012-02-10 16:51 +0000 |
| Message-ID | <jh3hue$rpo$1@reader1.panix.com> |
| In reply to | #17059 |
In <qNmdnSCgb8WN1KjSnZ2dnUVZ_oGdnZ2d@bestweb.net> Kenneth Brody <kenbrody@spamcop.net> writes:
> On 2/9/2012 4:16 PM, John Gordon wrote:
> > In<jh19o6$qnl$1@speranza.aioe.org> peter<pc722@nospam.mail.com.br> writes:
> >
> >> In fact, I want to remove all NULLS and EOFs (0x1a)
> [...]
> > C strings are terminated by a NULL character. Therefore, by definition,
> > you won't find any NULLs in the string itself.
> <nit type="not so minor">
> There is no such thing as a "NULL character" in C. Rather, strings are
> terminated by a "null character". The all-caps "NULL" is a macro
> representing the "null pointer".
> </nit>
Keith pointed out the same thing. I stand corrected!
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | "christian.bau" <christian.bau@cbau.wanadoo.co.uk> |
|---|---|
| Date | 2012-02-14 14:48 -0800 |
| Message-ID | <95093eb2-6115-45d7-ae11-d4d39f4c7de7@c21g2000yqi.googlegroups.com> |
| In reply to | #16999 |
On Feb 9, 9:16 pm, John Gordon <gor...@panix.com> wrote: > C strings are terminated by a NULL character. Therefore, by definition, > you won't find any NULLs in the string itself. On the other hand, the way the code is written, it will find the null character terminating the string, and then it will happily proceed into undefined behaviour.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-02-09 13:22 -0800 |
| Message-ID | <lnwr7vln10.fsf@nuthaus.mib.org> |
| In reply to | #16992 |
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?
There's probably no faster way than a for loop, but yours can be
improved considerably by not calling strlen() on each iteration.
strlen() has to scan the entire string, and you're doing that once for
each character.
Also, the correct condition is "<", not "<=". For example if the
string's value is "hello", then strlen() returns 5, but you want to
check positions 0 through 4.
const size_t len = strlen(buffer);
for (i = 0; i < len; i ++) {
...
}
And some terminology issues. NULL is (a macro that expands to)
a null *pointer* constant; the null character is better referred
to as NUL, or just '\0'. (Yes, some character set standards do
call it NULL, but using that name can be confusing.)
And EOF is a macro that expands to a negative integer constant
expression, typically (-1). 0x1A is the control-Z character,
which is used on some systems, to indicate an end-of-file condition.
Finally, strlen() searches for the '\0' character that marks the end
of a string. If your buffer might have multiple '\0' characters in
it, then it isn't a string, and you should use some other technique
to determine how long it is (or how long the relevant portion of
it is).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2012-02-09 14:41 -0800 |
| Message-ID | <432fca1c-9f0c-4d7d-a2bf-e7d91a54277d@f30g2000yqh.googlegroups.com> |
| In reply to | #16992 |
On Feb 9, 8:19 pm, peter <pc...@nospam.mail.com.br> wrote:
> 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?
>
Yes.
get the length of the data in the buffer. Only you can do that.
Probably you want to exclude the last terminating nul from the
replacement, but maybe not, depending on how you're going to use the
data. You might even ned to add a nul.
Then just do this.
len = data_length_got_somehow;
for(i=0;i<len;i++)
if(buffer[i] == 0 || buffer[i] == 0x1a)
buffer[i] = ' ';
/* possibly you need to do this, but make sure that buffer is one
bigger than len */
buffer[i] = 0;
If you call strlen() in the for control statement, the length of the
string will be reclaculated on each iteration, which is slow. Also
since you want to replace nuls, it's a bug.
--
Visit my website
http://www.malcolmmclean.site11.com/www
[toc] | [prev] | [next] | [standalone]
| From | Kenneth Brody <kenbrody@spamcop.net> |
|---|---|
| Date | 2012-02-10 11:48 -0500 |
| Message-ID | <S5qdnVvuy6XU16jSnZ2dnUVZ_sidnZ2d@bestweb.net> |
| In reply to | #16992 |
On 2/9/2012 3:19 PM, peter wrote:
> 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?
Since most of this has already been discussed elsethread, I'd like to point
out that the use of "<=strlen(buffer)" in the loop means that this loop will
never terminate until "something bad" happens.
Because of the "<=", the loop will, in fact, find the terminal '\0' of the
string, which will immediately be overwritten by a space. This, in turn,
causes strlen() to find the next null character (or do "something bad" if it
doesn't find one), and the loop continues, searching past the end of the
original string, overwriting any '\0' and 0x1a characters it may come across.
--
Kenneth Brody
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-02-10 11:08 -0800 |
| Message-ID | <lnd39mld5t.fsf@nuthaus.mib.org> |
| In reply to | #16992 |
peter <pc722@nospam.mail.com.br> writes:
> In fact, I want to remove all NULLS and EOFs (0x1a)
NULs and control-Zs
> from a string
from a buffer
> 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?
It's probably a good idea to investigate how those bytes got into
your buffer in the first place. Where did the data come from?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c
csiph-web