strpbrk()

A

amanayin

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;
}
 
D

Dan Pop

In said:
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
 
A

amanayin

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

Dan Pop

In said:
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
 
B

Ben Pfaff

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

Dan Pop

In said:
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
 
A

amanayin

/* 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
 
A

amanayin

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"
 
B

Ben Pfaff

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".
 
B

Ben Pfaff

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

CBFalconer

Ben said:
(e-mail address removed) (Dan Pop) writes:
.... snip ...


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

When 'perturbing' an existing program, the most important thing to
know is the scope of the perturbation. With luck the original
programmer has marked the routine static, and it communicates
solely through its parameters. In practice, however ....
 
D

Dan Pop

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

To emphasise "understand". Many people consider they understand
something when they have a very approximative idea about it.
*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 neither think so nor expressed this opinion. I was talking about
modifying programs for LEARNING purposes. Once you have learned C,
it is perfectly possible to use the hacking approach to program
maintenance. This is how I actually maintain all the software I
haven't written myself in the first place. The piece of code you
need to understand for this purpose is typically larger than a whole
program used by beginners who are in the process of learning the
language.

Try to keep your comments focused to the context of the discussion.

Dan
 

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

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top