Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strpbrk()

Reply
Thread Tools

strpbrk()

 
 
amanayin
Guest
Posts: n/a
 
      08-11-2003
size_t strcspn(char *str1, char *str2);
char *strpbrk(char *str, char *accept);

In the book i am using it says:

The function strpbrk() returns a pointer to the first
character in str that matches any of the characters in accept.
If it doesn't find a match, the function retuns NULL. As
previously explained for the function strchr(), you can
obtain the offset of the first match in str by subtracting
the pointer str from the pointer returned by strpbrk()
(if it isn't NULL, of cource).
For example, replace strcspn() on line 21 with strpbrk();

But when i do i get the following error
strcspn.c:21: warnig: assignment makes integer
from pointer without a cast

does this error have something to do with the functions
or have i missinterprtated what is said above could
some one please explain. And how do you use strpbrk()
properly

/* STRCSPN.C DEMONSTRATES THE STRSCPN() FUNCTION */

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

int main(void)
{
char buf1[80], buf2[80];
size_t loc;

/* INPUT THE STRINGS. */

printf("Enter the string to be searched: ");
fgets(buf1,80,stdin);
printf("Enter the string containing target characters: ");
fgets(buf2,80,stdin);
buf2[strlen(buf2)-1] = 0;

/* PERFORM THE SEARCH */

loc = strcspn(buf1, buf2);

if(loc == strlen(buf1))
printf("No match found\n");
else
printf("The first match was found at position %d.\n",loc);
return 0;
}

 
Reply With Quote
 
 
 
 
Dan Pop
Guest
Posts: n/a
 
      08-11-2003
In <bh7ola$ima$(E-Mail Removed)> amanayin <(E-Mail Removed)> writes:

>size_t strcspn(char *str1, char *str2);
>char *strpbrk(char *str, char *accept);
>
>In the book i am using it says:
>
>The function strpbrk() returns a pointer to the first
>character in str that matches any of the characters in accept.
>If it doesn't find a match, the function retuns NULL. As
>previously explained for the function strchr(), you can
>obtain the offset of the first match in str by subtracting
>the pointer str from the pointer returned by strpbrk()
>(if it isn't NULL, of cource).
>For example, replace strcspn() on line 21 with strpbrk();
>
>But when i do i get the following error
>strcspn.c:21: warnig: assignment makes integer
>from pointer without a cast
>
>does this error have something to do with the functions
>or have i missinterprtated what is said above could
>some one please explain. And how do you use strpbrk()
>properly


What is the return type of strcspn? What is the return type of strpbrk?
You can't make an arbitrary change to a C program and still expect it to
be a correct C program.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
amanayin
Guest
Posts: n/a
 
      08-12-2003
At this point in my learnig i lack the understanding so it
would be appreciated if you could take the program and
correct it for me so i have a example to go on this would
enable me to see what your taking about.

Thanks for your help.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-12-2003
In <bha8q9$2hl$(E-Mail Removed)> amanayin <(E-Mail Removed)> writes:

>At this point in my learnig i lack the understanding so it
>would be appreciated if you could take the program and
>correct it for me so i have a example to go on this would
>enable me to see what your taking about.


At any time in your learning process you should not play with programs
you don't fully understand!

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-12-2003
(E-Mail Removed) (Dan Pop) writes:

> At any time in your learning process you should not play with programs
> you don't fully understand!


Why not? That's the way to learn. If I hadn't spent lots and
lots of time playing with programs I didn't fully understand, I
wouldn't be a competent programmer today.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-13-2003
In <(E-Mail Removed)> Ben Pfaff <(E-Mail Removed)> writes:

>(E-Mail Removed) (Dan Pop) writes:
>
>> At any time in your learning process you should not play with programs
>> you don't fully understand!

>
>Why not? That's the way to learn.


It's the WRONG way to learn. If you don't understand the program, how can
you tell whether its output is correct by design or by accident? What can
you learn from such a program? What can you learn from the changes you
make to such a program? C is not the kind of language one can learn by
blindly experimenting.

The correct way to learn is to try to understand the program first.
Then, by playing with it, you check whether your understanding was
correct or not.

>If I hadn't spent lots and
>lots of time playing with programs I didn't fully understand, I
>wouldn't be a competent programmer today.


Or maybe you'd be a lot more competent today if you tried to understand
the programs you played with.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
amanayin
Guest
Posts: n/a
 
      08-13-2003
/* STRPBRK.C DEMONSTRATES THE STRPDRK() FUNCTION */
/* COME BACK TO THIS ONE */

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

int main(void)
{
char buf1[100], buf2[100];
char *p, *c;
int loc;

/* INPUT THE STRINGS. */

printf("Enter the string to be searched: ");
fgets(buf1+1,100,stdin);
buf1[strlen(buf1)-1] = 0;

printf("Enter the string containing target characters: ");
fgets(buf2,100,stdin);
buf2[strlen(buf2)-1] = 0;

/* ASSIGN POINTERS */

p = buf1;
c = buf2;

/* PERFORM THE SEARCH */

loc = strlen(p = strpbrk(p+1,c));

if(loc == 0)
printf("Not found\n");
else
printf("%u\n",loc);

return 0;
}

What have i done wrong when no match is found it gives the ouput
segmentation fault. If this code wants flaming, flame away but
put it in terms a begginer can understand nothing criptic please
 
Reply With Quote
 
amanayin
Guest
Posts: n/a
 
      08-13-2003
Owe i for got to say it counts back wards
example if i input first string "how now"
and i input second string "how"
out put reads "7"
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-13-2003
(E-Mail Removed) (Dan Pop) writes:

> In <(E-Mail Removed)> Ben Pfaff <(E-Mail Removed)> writes:
>
> >(E-Mail Removed) (Dan Pop) writes:
> >
> >> At any time in your learning process you should not play with programs
> >> you don't fully understand!

> >
> >Why not? That's the way to learn.

>
> It's the WRONG way to learn. If you don't understand the program, how can
> you tell whether its output is correct by design or by accident? What can
> you learn from such a program? What can you learn from the changes you
> make to such a program? C is not the kind of language one can learn by
> blindly experimenting.
>
> The correct way to learn is to try to understand the program first.
> Then, by playing with it, you check whether your understanding was
> correct or not.


In other words, you should play with programs you don't fully
understand. "Try to understand" != "fully understand".
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-14-2003
(E-Mail Removed) (Dan Pop) writes:

> In <(E-Mail Removed)> Ben Pfaff <(E-Mail Removed)> writes:
>
> >(E-Mail Removed) (Dan Pop) writes:
> >
> >> In <(E-Mail Removed)> Ben Pfaff <(E-Mail Removed)> writes:
> >>
> >> >(E-Mail Removed) (Dan Pop) writes:
> >> >
> >> >> At any time in your learning process you should not play with programs
> >> >> you don't fully understand!
> >> >
> >> >Why not? That's the way to learn.
> >>
> >> It's the WRONG way to learn. If you don't understand the program, how can
> >> you tell whether its output is correct by design or by accident? What can
> >> you learn from such a program? What can you learn from the changes you
> >> make to such a program? C is not the kind of language one can learn by
> >> blindly experimenting.
> >>
> >> The correct way to learn is to try to understand the program first.
> >> Then, by playing with it, you check whether your understanding was
> >> correct or not.

> >
> >In other words, you should play with programs you don't fully
> >understand. "Try to understand" != "fully understand".

>
> You either understand something or you don't.


If understanding is binary, black or white, why did you use the
phrase "fully understand"?

> Your understanding may not be correct, so you need to validate
> it by playing with the program. Trying to understand the
> program by playing with it is the hopelessly wrong aproach.


*shrug* Often works for me. If you think you need to understand
a program fully before trying to modify it, you haven't ever
needed to make a small modification to a large program.
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
 
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




Advertisments