Hi again,
I've got an issue here with stdio strlen function :
strlen is not declared in stdio.h.
#include <stdio.h>
#include <string.h>
int CharSpaces(char *Blabla) ;
int CntNrE(char *Blabla) ;
int LastWord(char *Blabla) ;
int main(void)
{
char Txt[200] ;
int cnt1, cnt2, cnt3 ;
int i ;
printf("\n\nEnter a sentence of less than 200 char. : \n") ;
fgets(Txt, 200, stdin) ;
You do remember that fgets will include a '\n' before the '\0' if
there is room for it?
Here you are counting the '\n' even though you can't see it.
printf("\n1-\tNumber of characters including spaces : %d \n", cnt1) ;
cnt2 = CharSpaces(Txt) ;
printf("\n2-\tNumber of characters only : %d \n", cnt1-cnt2) ;
return 0 ;
}
int CharSpaces(char *Blabla)
{
int i = 0 ;
int sp = 0 ;
while (*Blabla )
{
if ( isspace(*Blabla) )
You failed to include ctype.h so there is no declaration in scope for
isspace.
sp++ ;
Blabla++ ;
}
return sp -1 ; /* MY ISSUE here : I need to 1 out of sp so i
get the right number*/
I have no idea what you mean here. sp does contain the number of
characters considered to be white space. You do remember that '\n' is
one of these characters? If you wanted to count only blanks, you
should compare against ' ' and not use isspace.
}
Another question :
What is the difference between !str and *str in this expression : if (!
str || !*str) ?
I think but i'm not sure : !str sends a message to check if str as
!str doesn't send anything. It applies the ! operator to the operand
str. If str is a pointer (as appears to be the case from the rest of
your question), the expression evaluates to 1 if str is NULL and
evaluates to 0 in all other cases.
!*str applies the * operator to the operand string and then applies
the ! operator to the result. It first operation evaluates to the
value of the object str points to. The second operation evaluates to
1 if the first operation resulted in any form of 0 (0.0 for floating
point, NULL for pointer, and 0 for any type of integer) and evaluates
to 0 in all other cases.
In general, you can think of the expression !something as being the
same as the expression something==0.
The two expressions are barely related. However, as used in this if
statement, the first serves as a check to insure the second does not
invoke undefined behavior. Remember that the || operator has a short
circuit evaluation. If the left operand is not 0, the second is not
evaluated. The left operand is not 0 (in fact it is 1) only if str is
NULL and in that case you do not want to evaluate the right operand
since *str would be the same as *NULL which would invoke undefined
behavior. If str is not NULL, the assumption is that it points to a
valid object and *str is a valid expression.
The whole code is from this website :
http://www.codase.com/search/call?name=isspace
as I was looking for strlen code sample to see if it would return the
"unexpected" value i get.
The value is only unexpected because you forgot what fgets does with
the '\n'.
char **
_XParseBaseFontNameList(
char *str,
{
char *plist[XMAXLIST];
char **list;
char *ptr, *psave;
*num = 0;
if (!str || !*str) {
return (char **)NULL;
The cast serves no purpose.