By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,496 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,496 IT Pros & Developers. It's quick & easy.

Why the code doesn't work ?

P: n/a

bool isBlankLine(char *line) {

char *tmp=line;

bool blank=true;

while(*tmp++!='\0') {

if((*tmp!=' ') || (*tmp!='\n')) {

blank=false;

break;

}

}

return blank;
--
Posted via http://dbforums.com
Nov 13 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
dreamcatcher wrote:
[A bunch of code formatted to induce nausea.]

Please try to post in a form that can be read. Here is your code

bool isBlankLine(char *line)
{
char *tmp = line;
bool blank = true;
while (*tmp++ != '\0') {
if ((*tmp != ' ') || (*tmp != '\n')) {
blank = false;
break;
}
}
return blank;

Notice that those of us lucky enough to have <stdbool.h> would have to
#include it before even having a chance to try your code, and those with
C89(C90) compilers, which are the vast majority, would need to make up
definitions for bool, false, and true. Try to post code that we have a
chance in hell of even testing.

Then note that there is no closing right brace. This function does not
end. Now, I don't know what you consider a blank line. If you mean one
that has nothing but whitespace, here is a better approach. Note that the
name of the function is changed. Try very hard to avoid indentifiers
beginning with 'is', 'to', 'str', or 'E'. You could learn the rules for
when they are OK, but avoiding them is better. The name I use is from the
LISP idiom, where the 'p' stands for 'property'.

#include <ctype.h>

int blanklinep(char *s)
{
if (!s) return 0; /* null pointers can't point to
blank strings */
for(; *s && isspace(*s); s++)
/* nothing */ ;
/* The loop above ends when either *s is 0 or not whitespace,
for a 'blank' line, *s will always be 0 */
return !(*s);
}

--
Martin Ambuhl

Nov 13 '05 #2

P: n/a
On Sun, 14 Sep 2003 00:26:01 -0400, dreamcatcher
<me*********@dbforums.com> wrote:

bool isBlankLine(char *line) {
char *tmp=line;
bool blank=true;

while(*tmp++!='\0') {
This increments tmp, so when *tmp is accessed from the
if statement below it accesses the character after the
one you want.
if((*tmp!=' ') || (*tmp!='\n')) {
This never checks the first character of the string;
Worse, it does check the nul termination character
so this will always produce a non-blank string.

Also || is the logical OR operator. You want &&, the
logical AND operator.
blank=false;
break;
}
}
return blank;


Finally, you may it easier to use isspace() from <ctype.h>

Nick.

Nov 13 '05 #3

P: n/a
Martin Ambuhl <ma*****@earthlink.net> wrote:
[...]
Then note that there is no closing right brace. This function does not
end. Now, I don't know what you consider a blank line. If you mean one
that has nothing but whitespace, here is a better approach. Note that the
name of the function is changed. Try very hard to avoid indentifiers
beginning with 'is', 'to', 'str', or 'E'. You could learn the rules for
when they are OK, but avoiding them is better. The name I use is from the
LISP idiom, where the 'p' stands for 'property'.

#include <ctype.h>

int blanklinep(char *s)
{
if (!s) return 0; /* null pointers can't point to
blank strings */
for(; *s && isspace(*s); s++)
Since *s is a char, you have to cast to unsigned char here (isspace
takes an integer with a value in the range of unsigned char):

for (; *s && isspace((unsigned char)*s); s++)
/* nothing */ ;
/* The loop above ends when either *s is 0 or not whitespace,
for a 'blank' line, *s will always be 0 */
return !(*s);
}


- Kevin.

Nov 13 '05 #4

P: n/a

"Nick Austin" <ni**********@nildram.co.uk> wrote in message
if((*tmp!=' ') || (*tmp!='\n')) {


Also || is the logical OR operator. You want &&, the
logical AND operator.

This is a common error. In common speech we say if the fruit isn't apples or
the fruit isn't bananas, then sell it as cherries.
In computer language this must be if the fruit isn't apples AND the fruit
isn't bananas, then sell it as cherries.

Nov 13 '05 #5

P: n/a
Martin Ambuhl wrote:
... Try very hard to avoid indentifiers
beginning with 'is', 'to', 'str', or 'E'. You could learn the rules for
when they are OK, but avoiding them is better.


Hi Martin, would you elaborate on that, please? Is this a practice
intended to avoid namespace collision with the functions in
<ctype.h>? A style issue? Or what?

Thanks,
--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #6

P: n/a
In 'comp.lang.c', Tom Zych <tz******@pobox.com> wrote:
... Try very hard to avoid indentifiers
beginning with 'is', 'to', 'str', or 'E'. You could learn the rules for
when they are OK, but avoiding them is better.


Hi Martin, would you elaborate on that, please? Is this a practice
intended to avoid namespace collision with the functions in
<ctype.h>? A style issue? Or what?


These identifiers are reserved for future extensions.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #7

P: n/a


dreamcatcher wrote:
bool isBlankLine(char *line) {
From the looks of this function, I would make it
const char *line

char *tmp=line;

bool blank=true;

while(*tmp++!='\0') {
You are incrementing before you test.
if((*tmp!=' ') || (*tmp!='\n')) {
I believe you want the && operator instead of the || operator.

blank=false;

break;

}

}

return blank;


/* Testing */
#include <stdio.h>

typedef enum bool {false, true} bool;

bool isBlankLine(const char *line)
{
bool blank;

for( blank = true; *line != '\0' && blank != false;line++)
if((*line !=' ') && (*line !='\n'))
blank=false;
return blank;
}

int main(void)
{
char *str1 = " a ";
char *str2 = " ";

printf("\"%s\" is%s all blanks\n",str1,
isBlankLine(str1)?"":" not");
printf("\"%s\" is%s all blanks\n",str2,
isBlankLine(str2)?"":" not");
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #8

P: n/a


Tom Zych wrote:
Martin Ambuhl wrote:

... Try very hard to avoid indentifiers
beginning with 'is', 'to', 'str', or 'E'. You could learn the rules for
when they are OK, but avoiding them is better.

Hi Martin, would you elaborate on that, please? Is this a practice
intended to avoid namespace collision with the functions in
<ctype.h>? A style issue? Or what?


I would say that they are reserved for possible future library usage.
The specific question here, using "is" is found in section
7.26 FURURE LIBRARY DIRECTIONS.

The following names are grouped under individual headers for
convenience. All external names described below are reserved
no matter what headers are included by the program.

.....snip........

7.26.2 Character handling <ctype.h>
1 Function names that begin with either is or to, and a lowercase
letter may be added to the declarations in the <ctype.h> header.

The function defined by the op, named "isBlankLine", is OK and is
not reserved. If the op had given the function the name "isblankline",
then you are in collison with the standard reserved names.

I believe Martin is simply suggesting not to use any functions that
begin with "is" because this rule and the other rules is often not
understood or forgotten. This suggestion is a matter of opinion and
preference. IMO the name "isBlankLine" is a good because it is
elucid in describing the function's usage.

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #9

P: n/a
Malcolm <ma*****@55bank.freeserve.co.uk> wrote:

"Nick Austin" <ni**********@nildram.co.uk> wrote in message
>if((*tmp!=' ') || (*tmp!='\n')) {
Also || is the logical OR operator. You want &&, the
logical AND operator.

This is a common error. In common speech we say if the fruit isn't apples or
the fruit isn't bananas, then sell it as cherries.


That's really sloppy use of language though - shouldn't it be "if the
fruit is neither apples nor bananas, then..." ?

- Kevin.
In computer language this must be if the fruit isn't apples AND the fruit
isn't bananas, then sell it as cherries.

Nov 13 '05 #10

P: n/a
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
This is a common error. In common speech we say if the fruit isn't
apples or the fruit isn't bananas, then sell it as cherries.
That's just poor English grammar.
In computer language this must be if the fruit isn't apples AND
the fruit isn't bananas, then sell it as cherries.


And it is so in proper English too. Or, the also-correct
"if the fruit is neither apples nor bananas".

--
Simon.
Nov 13 '05 #11

P: n/a
On Mon, 15 Sep 2003 09:12:33 GMT, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote:
Malcolm <ma*****@55bank.freeserve.co.uk> wrote: <snip: && vs ||>
This is a common error. In common speech we say if the fruit isn't apples or
the fruit isn't bananas, then sell it as cherries.

I've never heard that (in the US). I have heard fairly often (the
equivalent of) "if the fruit isn't apples or oranges", which is meant
to parse irregularly as {if not {(the fruit is apples} or {[the fruit
is] oranges}}} where {} are grouping and [] are elision.
That's really sloppy use of language though - shouldn't it be "if the
fruit is neither apples nor bananas, then..." ?

Agree there.
- David.Thompson1 at worldnet.att.net
Nov 13 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.