hi all,
i am writing a program in which i am checking if a string contains another string(same what strstr does).
i assumed intially that the string1 is a long string with spaces and string2 is a word(which might be or might not be present in string1)
i have used strtok to seperate the words in string1 to be checked using strcmp.
its a very simple logic, but the problem is its showing segmentation fault.
can anyone find me a solution.

Member Avatar for iamthwee

Please post your code...

strtok() is not what you want. The text in string2 may or may not be a complete word surrounded with spaces. It could be a few letters of some larger word. Better if you just search string1 for the first character of string2. When (and if) found check if the rest of the characters in string2 follow. If not, then continue searching string1 for first character in string2.

If you know pointers then the job is a little easier. If not then you can use loop counters to index into the two strings.

>its a very simple logic
You'd think, but that might not be the case.

>but the problem is its showing segmentation fault.
I'd wager my next pay check that you're trying to strtok a string literal.

>if you just search string1 for the first character of string2. When (and if) found check if the rest of the characters in string2 follow

i don't wanna do it characterwise, and i am using pointers to implement it.

>you're trying to strtok a string literal.

i couldn't get you, strtok is used for string literals

strstr() is just strncmp() and a for loop.

strncmp(&haystack[0],needle,strlen(needle))
strncmp(&haystack[1],needle,strlen(needle))
and so on

# include <stdio.h>
int  main()
{
char *s1;
int found=0;
char *s2;
char * p3=NULL;
scanf("%s",&s1);
scanf("%s",&s2);
printf("hgf");
*p3=strtok(s2," ");
while(p3!=NULL)
{
if(strcmp(s1,p3))found=1;
strtok(NULL," ");
}
if(found)printf("yes");
return;
}
~

this is my code.kindly help:S

I'd wager my next pay check that you're trying to strtok a string literal.

# include <stdio.h>
int  main()
{
char *s1;
int found=0;
char *s2;
char * p3=NULL;
scanf("%s",&s1);
scanf("%s",&s2);
printf("hgf");
*p3=strtok(s2," ");
while(p3!=NULL)
{
if(strcmp(s1,p3))found=1;
strtok(NULL," ");
}
if(found)printf("yes");
return;
}
~

Can I have it? Pleeease? :icon_mrgreen:

As for the code, yuck. Even your input isn't correct, try allocating some memory first. And DO please listen to Salem's last suggestion, he basically handed you the solution.

>As for the code, yuck

i am new to it, if you could please be a bit kind not to use those words for my code

He didn't really mean it.

As far as program is concerned, read this, this and this.

>i am new to it, if you could please be a bit kind not to use those words for my code
We're not going to blow sunshine up your butt just because you're new. Eventually someone is going to tell you that you're a bad programmer, and it's better that you hear it sooner than later so that you can make improvements. A better reaction would be to ask us why your code is bad and how you can improve it.

He didn't really mean it.

As far as program is concerned, read this, this and this.

thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.