Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Replacing NULLS with space (C strings)

Reply
Thread Tools

Replacing NULLS with space (C strings)

 
 
Keith Thompson
Guest
Posts: n/a
 
      02-10-2012
Malcolm McLean <(E-Mail Removed)> writes:
> On Feb 10, 12:22*am, pete <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      02-10-2012
On 2012-02-10, Keith Thompson <(E-Mail Removed)> wrote:
> Malcolm McLean <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      02-10-2012
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.
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      02-10-2012
On Feb 10, 2:05*am, Kaz Kylheku <(E-Mail Removed)> wrote:
> On 2012-02-10, Keith Thompson <(E-Mail Removed)> wrote:
>
> > Malcolm McLean <(E-Mail Removed)> 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





 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      02-10-2012
In <(E-Mail Removed)> Kenneth Brody <(E-Mail Removed)> writes:

> On 2/9/2012 4:16 PM, John Gordon wrote:
> > In<jh19o6$qnl$(E-Mail Removed)> peter<(E-Mail Removed)> 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
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-10-2012
peter <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      02-11-2012
On 2/10/2012 14:08, Keith Thompson wrote:
> peter<(E-Mail Removed)> 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?
>


So far, we haven't seen much in the way of follow-ups from peter.
Multiple people have asked about the books regarding "pointer versus
array," but the lack of a peter-response doesn't prevent people from
continuing to invest time in responses to peter's queries, which is
fortunate for peter.
 
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
Newbie question: replacing nulls in CSV with preceding value Matt Waite Python 4 02-01-2007 10:31 PM
Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok?? Christian Seberino Python 21 10-27-2003 04:20 PM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-24-2003 11:15 AM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-23-2003 07:07 AM
Stack space, global space, heap space Shuo Xiang C Programming 10 07-11-2003 07:30 PM



Advertisments