Why the code doesn't work ?

D

dreamcatcher

bool isBlankLine(char *line) {

char *tmp=line;

bool blank=true;



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

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

blank=false;

break;

}

}

return blank;
 
M

Martin Ambuhl

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);
}
 
N

Nick Austin

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.
 
K

Kevin Easton

Martin Ambuhl said:
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.
 
M

Malcolm

Nick Austin said:
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.
 
T

Tom Zych

Martin said:
... 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,
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
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.
 
A

Al Bowers

dreamcatcher said:
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;
}
 
A

Al Bowers

Tom said:
Martin Ambuhl wrote:




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.
 
K

Kevin Easton

Malcolm said:
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.
 
S

Simon Biber

Malcolm said:
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".
 
D

Dave Thompson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top