Help beginner with simple string function

B

bpascal123

Hi,

I don't understand why i get a segmentation fault here. I'm trying to
reproduce isspace function...

Thanks,
Pascal


#include <stdio.h>
#include <string.h>

int CharLessSpaces(char *Blabla) ;

int main(void)
{
char Txt[200] ;
int cnt1, cnt2 ;

printf("\n\nEnter a sentence of less than 200 char. : \n") ;
fgets(Txt, 200, stdin) ;

cnt1 = strlen(Txt) ;
printf("\n\n1-\tNumber of characters including spaces : %d \n",
cnt1) ;

cnt2 = CharLessSpaces(Txt) ;
printf("\n2-\tNumber of blank spaces : %d \n\n", cnt2) ;

return 0 ;
}

int CharLessSpaces(char *Blabla)
{
int i, sp ;

/* manual : w/out isspace) */

while (Blabla )
{
if ( Blabla == ' ')
sp++;
i++ ;
}
return i ;
}

Thanks
 
M

Mark Storkamp

Hi,

I don't understand why i get a segmentation fault here. I'm trying to
reproduce isspace function...

Thanks,
Pascal


#include <stdio.h>
#include <string.h>

int CharLessSpaces(char *Blabla) ;

int main(void)
{
char Txt[200] ;
int cnt1, cnt2 ;

printf("\n\nEnter a sentence of less than 200 char. : \n") ;
fgets(Txt, 200, stdin) ;

cnt1 = strlen(Txt) ;
printf("\n\n1-\tNumber of characters including spaces : %d \n",
cnt1) ;

cnt2 = CharLessSpaces(Txt) ;
printf("\n2-\tNumber of blank spaces : %d \n\n", cnt2) ;

return 0 ;
}

int CharLessSpaces(char *Blabla)
{
int i, sp ;

/* manual : w/out isspace) */

while (Blabla )
{
if ( Blabla == ' ')
sp++;
i++ ;
}
return i ;
}

Thanks


i is never initialized (nor is sp).
 
B

bpascal123

I don't understand why i get a segmentation fault here. I'm trying to
reproduce isspace function...

#include <stdio.h>
#include <string.h>
int CharLessSpaces(char *Blabla) ;
int main(void)
{
   char Txt[200] ;
   int cnt1, cnt2 ;
   printf("\n\nEnter a sentence of less than 200 char. : \n") ;
   fgets(Txt, 200, stdin) ;
   cnt1 = strlen(Txt) ;
   printf("\n\n1-\tNumber of characters including spaces : %d \n",
cnt1) ;
   cnt2 = CharLessSpaces(Txt) ;
   printf("\n2-\tNumber of blank spaces : %d \n\n", cnt2) ;
   return 0 ;
}
int CharLessSpaces(char *Blabla)
{
   int i, sp ;
   /* manual : w/out isspace) */
   while (Blabla )
   {
           if ( Blabla == ' ')
                   sp++;
           i++ ;
   }
   return i ;
}


=o=

i is never initialized (nor is sp).


Hi Mark,
I was certain this instruction :

int i, sp ;

would by default assign 0 to i and sp.
It's now working with this modification following your help.
Thanks,
Pascal

int CharLessSpaces(char *Blabla)
{
int i = 0 ;
int sp = 0 ;

/* manual : w/out isspace) */

while (Blabla )
{
if ( Blabla == ' ')
sp++;
i++ ;
}
return sp++ ;
}
 
J

jameskuyper

....
i is never initialized (nor is sp).
....
I was certain this instruction :

int i, sp ;

would by default assign 0 to i and sp.[/QUOTE]

That would only be true if the declaration occurred at file scope, or
if you added the 'static' keyword.
 
B

bpascal123

     You don't mention what C compiler you're using, but many
compilers are able to warn you about using a variable without
first giving it a value.  You may need to "crank up the warning
level" to get this to happen; the popular gcc compiler can do
it, but not in its default mode of operation.  If you're using
gcc, try adding the flags "-W -Wall -O2" when you compile.
"-ansi -pedantic" (or "-std=c99 -pedantic") are also good.


Thanks
 
B

bpascal123

Hi again,

I've got an issue here with stdio strlen function :

#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) ;

cnt1 = strlen(Txt) ;
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) )
sp++ ;
Blabla++ ;
}
return sp -1 ; /* MY ISSUE here : I need to 1 out of sp so i
get the right number*/
}



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

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.

char **
_XParseBaseFontNameList(
char *str,
{
char *plist[XMAXLIST];
char **list;
char *ptr, *psave;
*num = 0;

if (!str || !*str) {
return (char **)NULL;
}

Thanks for your help,
Pascal
 
B

bpascal123

Hi again,

I've got an issue here with stdio strlen function :

#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) ;

        cnt1 = strlen(Txt) ;
        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) )
                        sp++ ;
                Blabla++ ;
        }
        return sp -1 ;       /* MY ISSUE here : I need to 1 out of sp so i
get the right number*/

}

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

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.

char **
_XParseBaseFontNameList(
    char           *str,
{
    char           *plist[XMAXLIST];
    char          **list;
    char           *ptr, *psave;
    *num = 0;

    if (!str || !*str) {
        return (char **)NULL;
    }

Thanks for your help,
Pascal

First question solved after looking at
file:////home/bpascal123/.mozilla/firefox/et6iqhw5.default//Read-It-Later%20Offline//RIL%20906.html,
it seems it was taking '\n' in consideration

and :

This routine tests whether a character is one of the standard white-
space characters, as follows:

space "\0"
horizontal tab \t
vertical tab \v
carriage return \r
new-line \n
form-feed \f

Ok, sorry for that - the next question is still pending if someone has
gotten a simple answer...
I don't know if it's an impression but it seems learning C requires a
lot of attention and time and the more one learns, the more there is
to learn. When i start learning when i have time, i think it will
about climbing a hill but i soon find myself high altitude in a
vertical position and that's only the beginning. However, i feel
things start to make sense ... slowly
Take it easy,
 
B

Beej Jorgensen

What is the difference between !str and *str in this expression : if (!
str || !*str) ?

char *str,

if (!str || !*str) {

What if I rewrite it in an equivalent and hopefully clearer form like
this:

if (str == NULL || *str == '\0')

It's basically checking if str is NULL or if str points to an empty
string (i.e. the character str is pointing to is '\0', the string
terminator).

Though I personally prefer the way I wrote it, !str and !*str are quite
idiomatic and legal.

-Beej
 
B

Barry Schwarz

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?
cnt1 = strlen(Txt) ;

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

song

u need initilize the variables in CharLessSpaces()
set i = 0;
and sp also need initilize,but i don't know why u need this variable

int CharLessSpaces(char *Blabla)
{
int i, sp ;

/* manual : w/out isspace) */


while (Blabla )
{
if ( Blabla == ' ')
sp++;
i++ ;
}
return i ;
}
 
N

Nick Keighley

your function (even with the bugs fixed) doesn't do the same thing as
isspace()

if you put the definition for CharLessSpaces() before main() you
wouldn't need the above declaration (aka prototype)

int main(void)
{
   char Txt[200] ;
   int cnt1, cnt2 ;
   printf("\n\nEnter a sentence of less than 200 char. : \n") ;
   fgets(Txt, 200, stdin) ;

may read trailing '\n'

int CharLessSpaces(const char *Blabla)

{
   int i, sp ;
   /* manual : w/out isspace) */
   while (Blabla )
   {
           if ( Blabla == ' ')
                   sp++;
           i++ ;
   }
   return i ;


this return one more than the number of characters. The value
of sp is discarded. Did you mean to return sp?
I was certain this instruction :

int i, sp ;

would by default assign 0 to i and sp.

you were wrong
It's now working with this modification following your help.

int CharLessSpaces(char *Blabla)
{
        int i = 0 ;
        int sp = 0 ;

        /* manual : w/out isspace) */

        while (Blabla )
        {
                if ( Blabla == ' ')
                        sp++;
                i++ ;
        }
        return sp++ ;


why the post inc? This is effectivly equivalent to

return sp;
sp = sp + 1;

it returns the value sp then increments sp. Why?

--
Nick Keighley

It's probably not the quickest method around, but it uses plenty of
trees, and that's what counts.
Richard Heathfield
 
N

Nick Keighley

        cnt2 = CharLessSpaces(Txt) ;
        printf("\n2-\tNumber of blank spaces : %d \n\n", cnt2) ;

is CharLessSpaces() intended to return the number of charcaters minus
the number of spaces (as its names indicates)? or is it intended to
return
the number of spaces as the text indicates? Decide what it does and
name it
appropriatly.
 
B

bpascal123

u need initilize the variables in CharLessSpaces()
set i = 0;
and sp also need initilize,but i don't know why u need this variable

Hi Song,

This is a "while" loop and from its mechanism i don't see how to use
one variable. "While" needs to be incremented whatever the "if"
condition says. But the "if" condition needs its variable. As a
beginner, that's the way i see this.

This loop below uses a pointer in which "Blabla" is incremented and
for me it means that it would be very difficult to make the economy of
one variable in both loop unless there is some C tricks i am really
not ready to deal with at this stage...

while (*Blabla )
{
if ( isspace(*Blabla) )
sp++ ;
Blabla++ ;
}
return sp ;

Thanks for your interest,

Pascal
 
B

bpascal123

Hi Barry,
strlen is not declared in stdio.h.
+++++++++++++++++++++
+++++++++++++++++++++

I just got confused because I was reading stuff about stdio i think on
wiki and it was saying that \n (new line) could be preceded by a white
space but it's up to the system implementation ( ISO or something). At
the time i was dealing with this issue i was compiling from gcc linux
ubuntu i wasn't sure if stdio wasn't the cause of this. But my fault,
i should have written string.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?


   cnt1 = strlen(Txt) ;

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.

++++++++++++++++++++++
+++++++++++++++++++++

The fact is that it's compiling without adding ctype at least on
Windows. I didn't try on linux since i'm working from Windows today.

++++++++++++++++++++++
+++++++++++++++++++++

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.




!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.
++++++++++++++++++++++
+++++++++++++++++++++

I'll keep your paragraph above in my memo. Thanks

++++++++++++++++++++++
+++++++++++++++++++++
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.
 
N

Nick Keighley

This is a "while" loop and from its mechanism i don't see how to use
one variable. "While"

"while" is never incremented
needs to be incremented whatever the "if"
condition says. But the "if" condition needs its variable. As a
beginner, that's the way i see this.

I'd probably code it your way
This loop below uses a pointer in which "Blabla" is incremented and
for me it means that it would be very difficult to make the economy of
one variable in both loop

what do tou mean "both loop"? There is only one loop

unless there is some C tricks i am really
not ready to deal with at this stage...

can't say I don't blame you
        while (*Blabla )
        {
                if ( isspace(*Blabla) )
                        sp++ ;
                Blabla++ ;
        }
        return sp ;

your original code didn't return sp but i. This may be why
song thought sp was unnecessary
 
B

bpascal123

"while" is never incremented


I'd probably code it your way


what do tou mean "both loop"? There is only one loop

<<<<<<<<<

I meant the loop from the first post which was slightly different
because of Blabla as an array and *Blabla as a pointer (i need to feel
more comfortable with those subtilities that makes coding easier...) :

while (Blabla )
{
if ( Blabla == ' ')
sp++;
i++ ;
}
return sp ;
can't say I don't blame you


your original code didn't return sp but i. This may be why
song thought sp was unnecessary

<<<<<<<<<<<<<<<<<

return i ; it was a mistake i changed in between your post and latest
post. Thanks
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top