469,927 Members | 1,863 Online

# need some help

hi friends,
i have some questions whch is in my last year question papers.i need
some help to get logic of these questions.

1) write a C function, that takes two strings as arguments and returns
a pointer to the first occurrence of 1st string in 2nd string or NULL
if it is not present.

-- i tried to solve it but it seems that i am not understanding this
question at all.i am taking this question as:

1st string- "cat"
2nd string-"i like cat."

i have to return pointer that has strarting address of 'c' of cat of
2nd string.i can make a program ( that finds a string into another )
but how to return pointer of that string i don`t know.

2) write a 'C' function
the function reads "n" strings from the input and creates a list of
such strings dynamically using "malloc" library call.
-- as well as i understand i have to make a linked list that will hold
the strings(i can make it )but one thing i didn`t understand what this
function will return, Address of first node of that list or something
else.if yes please describe how?

my english is not good but i think you have understood my problems.if
anyone can suggest a good link related to my problems, i will be
thankful.
:)

Jun 9 '06 #1
19 1825
ash said:
hi friends,
i have some questions whch is in my last year question papers.i need
some help to get logic of these questions.

1) write a C function, that takes two strings as arguments and returns
a pointer to the first occurrence of 1st string in 2nd string or NULL
if it is not present.
Well, that one's easy, at any rate.

#include <string.h>
char *ashstrstr(char *haystack, char *needle)
{
return strstr(haystack, needle);
}
2) write a 'C' function
the function reads "n" strings from the input and creates a list of
such strings dynamically using "malloc" library call.
-- as well as i understand i have to make a linked list that will hold
the strings(i can make it )but one thing i didn`t understand what this
function will return, Address of first node of that list or something
else.if yes please describe how?

If it were asking for a linked list, it would have said so, and there would
have been some kind of linked list thingy in the prototype, and it didn't
and there isn't so it isn't.

No, what it's looking for is this:

1) set up your array of n char * objects, like this:

char **new = malloc(n * sizeof *new);
if(new != NULL)
{

2) the next stage is to write (or find) a function that can read an entire
line from standard input, reallocating storage as and when necessary to
ensure that there is sufficient room to store the string. For a very simple
way to do this that will suit you very well, look for Chuck Falconer's
ggets() function which, last I heard, could be found at:

3) simply call this function in a loop, assigning each pointer thus obtained
to new[i], where i is your loop counter, running from 0 to n-1. What will
you do if the function returns NULL, to indicate insufficient storage, or
perhaps an absence of input data?

4) return new;
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 9 '06 #2

"Richard Heathfield" <in*****@invalid.invalid> wrote in message
news:dc********************@bt.com...
ash said:
hi friends,
i have some questions whch is in my last year question papers.i need
some help to get logic of these questions.

1) write a C function, that takes two strings as arguments and returns
a pointer to the first occurrence of 1st string in 2nd string or NULL
if it is not present.
Well, that one's easy, at any rate.

#include <string.h>
char *ashstrstr(char *haystack, char *needle)
{
return strstr(haystack, needle);
}

This is wrong. It should be strstr(needle, haystack).
The instruction was to find first occurence of s1 in s2.
strstr finds first s2 in s1.

For this homework, I think the instructor really wants
the student to write the internals of strstr. If so, he/she
should have explicitly stated that using strstr was not allowed.
2) write a 'C' function
the function reads "n" strings from the input and creates a list of
such strings dynamically using "malloc" library call.
-- as well as i understand i have to make a linked list that will hold
the strings(i can make it )but one thing i didn`t understand what this
function will return, Address of first node of that list or something
else.if yes please describe how?

If it were asking for a linked list, it would have said so, and there
would
have been some kind of linked list thingy in the prototype, and it didn't
and there isn't so it isn't.

No, what it's looking for is this:

1) set up your array of n char * objects, like this:

char **new = malloc(n * sizeof *new);
if(new != NULL)
{

2) the next stage is to write (or find) a function that can read an entire
line from standard input, reallocating storage as and when necessary to
ensure that there is sufficient room to store the string. For a very
simple
way to do this that will suit you very well, look for Chuck Falconer's
ggets() function which, last I heard, could be found at:

3) simply call this function in a loop, assigning each pointer thus
obtained
to new[i], where i is your loop counter, running from 0 to n-1. What will
you do if the function returns NULL, to indicate insufficient storage, or
perhaps an absence of input data?

4) return new;
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Jun 9 '06 #3
"Richard Heathfield" wrote:
ash said:
hi friends,
i have some questions whch is in my last year question papers.i need
some help to get logic of these questions.

1) write a C function, that takes two strings as arguments and returns
a pointer to the first occurrence of 1st string in 2nd string or NULL
if it is not present.

Well, that one's easy, at any rate.

#include <string.h>
char *ashstrstr(char *haystack, char *needle)
{
return strstr(haystack, needle);
}

You know perfectly well that that is not what the instructor wanted.
Jun 9 '06 #4
Fred Kleinschmidt said:

"Richard Heathfield" <in*****@invalid.invalid> wrote in message
news:dc********************@bt.com...
ash said:
hi friends,
i have some questions whch is in my last year question papers.i need
some help to get logic of these questions.

1) write a C function, that takes two strings as arguments and returns
a pointer to the first occurrence of 1st string in 2nd string or NULL
if it is not present.
Well, that one's easy, at any rate.

#include <string.h>
char *ashstrstr(char *haystack, char *needle)
{
return strstr(haystack, needle);
}

This is wrong. It should be strstr(needle, haystack).

<sigh> The easy ones are always hardest.
The instruction was to find first occurence of s1 in s2.
strstr finds first s2 in s1.
Quite right. My apologies.
For this homework, I think the instructor really wants
the student to write the internals of strstr.
Yes, but here in comp.lang.c we try to get the OP to think, rather than
spoonfeed them a solution. In this case, I was trying to get him (or
possibly her - I haven't checked) to think about how to phrase the
question. Shame I messed up the answer, though...
If so, he/she
should have explicitly stated that using strstr was not allowed.

Precisely so.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 9 '06 #5

one friend advised me to use "strstr" function, this is a easy way to
solve that question by use built in function but actually i was trying
to make this function and i want help in writing that function.

Jun 9 '06 #6

one friend advised me to use "strstr" function, this is a easy way to
solve that question by use built in function but actually i was trying
to make this function and i want help in writing that function.

Jun 9 '06 #7
ash wrote:

one friend advised me to use "strstr" function

That's nice. See below (it's still not clear to me which Google sites
are "fixed" and which aren't).

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
Jun 9 '06 #8
Richard Heathfield wrote:
.... snip ...
2) the next stage is to write (or find) a function that can read
an entire line from standard input, reallocating storage as and
when necessary to ensure that there is sufficient room to store
the string. For a very simple way to do this that will suit you
very well, look for Chuck Falconer's ggets() function which,
last I heard, could be found at:

3) simply call this function in a loop, assigning each pointer
thus obtained to new[i], where i is your loop counter, running
from 0 to n-1. What will you do if the function returns NULL, to
indicate insufficient storage, or perhaps an absence of input data?

int ggets(char**) returns 0 for success, EOF for eof, and positive
for lack of memory. So a suitable read'em'all loop is:

while (0 == ggets(&buffptr)) { ... }

--
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

Jun 10 '06 #9
CBFalconer said:
Richard Heathfield wrote:

... snip ...

For a very simple way to do this that will suit you
very well, look for Chuck Falconer's ggets() function which,
last I heard, could be found at:

3) simply call this function in a loop, assigning each pointer
thus obtained to new[i], where i is your loop counter, running
from 0 to n-1. What will you do if the function returns NULL, to
indicate insufficient storage, or perhaps an absence of input data?

int ggets(char**) returns 0 for success, EOF for eof, and positive
for lack of memory. So a suitable read'em'all loop is:

while (0 == ggets(&buffptr)) { ... }

Ah, I had forgotten that. Thank you, Chuck.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 10 '06 #10
Hi
u can use strstr function which returns int telling position of first
occurance of one string in other string
by using that position(index) u can return poiter.
ash wrote:
hi friends,
i have some questions whch is in my last year question papers.i need
some help to get logic of these questions.

1) write a C function, that takes two strings as arguments and returns
a pointer to the first occurrence of 1st string in 2nd string or NULL
if it is not present.

-- i tried to solve it but it seems that i am not understanding this
question at all.i am taking this question as:

1st string- "cat"
2nd string-"i like cat."

i have to return pointer that has strarting address of 'c' of cat of
2nd string.i can make a program ( that finds a string into another )
but how to return pointer of that string i don`t know.

2) write a 'C' function
the function reads "n" strings from the input and creates a list of
such strings dynamically using "malloc" library call.
-- as well as i understand i have to make a linked list that will hold
the strings(i can make it )but one thing i didn`t understand what this
function will return, Address of first node of that list or something
else.if yes please describe how?

my english is not good but i think you have understood my problems.if
anyone can suggest a good link related to my problems, i will be
thankful.
:)

Jun 10 '06 #11
Hi
u can use strstr function which returns int telling position of first
occurance of one string in other string
by using that position(index) u can return poiter.
ash wrote:
hi friends,
i have some questions whch is in my last year question papers.i need
some help to get logic of these questions.

1) write a C function, that takes two strings as arguments and returns
a pointer to the first occurrence of 1st string in 2nd string or NULL
if it is not present.

-- i tried to solve it but it seems that i am not understanding this
question at all.i am taking this question as:

1st string- "cat"
2nd string-"i like cat."

i have to return pointer that has strarting address of 'c' of cat of
2nd string.i can make a program ( that finds a string into another )
but how to return pointer of that string i don`t know.

2) write a 'C' function
the function reads "n" strings from the input and creates a list of
such strings dynamically using "malloc" library call.
-- as well as i understand i have to make a linked list that will hold
the strings(i can make it )but one thing i didn`t understand what this
function will return, Address of first node of that list or something
else.if yes please describe how?

my english is not good but i think you have understood my problems.if
anyone can suggest a good link related to my problems, i will be
thankful.
:)

Jun 10 '06 #12

"ash" <as************@rediffmail.com> wrote

one friend advised me to use "strstr" function, this is a easy way to
solve that question by use built in function but actually i was trying
to make this function and i want help in writing that function.

First write the skeleton

/*
mystrstr - find the first occurrence of substring in string
Params: substring - string to search for
string - string to search in.
Returns: pointer to occurence of substring in string, NULL if none found.
*/
char *mystrstr(char *substring, char *string)
{
}

Now for the algorithm.

Step through string until you find a character that matches the first
character of substring.
If you reach the end of the string, return NULL.
Now step through substring comparing every character to the following
characters in string. If you get a mismatch, abort and continue stepping
through string.
If you get to the NUL at the end of substring, you have found a perfect
match. Return the pointer to the start of the substring.

Here's a test funtion.

int main(void)
{
char *sub;

sub = mystrstr("Fred", "My name is Fred and I am dead");
/* should print out "Fred and I am dead"
printf("%s\n", sub);
sub = mystrstr("Frederick", "My name is Fred and I am dead"):
/* should print out that sub is null */
printf("%p\n", sub);
sub = mystrstr("Frederick", "Fred");
/* should print out that sub is null */
printf("%p\n", sub);
sub = mystrstr("Fred", "Fred, is Fred dead?");
/* should print out "Fred, is Fred dead?" */
printf("%s\n", sub);
sub = mystrstr("Fred", sub);
/* should print out "Fred dead?");
printf("%s\n", sub);
return 0;
}

--
Buy my book 12 Common Atheist Arguments (refuted)
\$1.25 download or \$7.20 paper, available www.lulu.com/bgy1mm
Jun 10 '06 #13
Malcolm schrieb:
"ash" <as************@rediffmail.com> wrote
one friend advised me to use "strstr" function, this is a easy way to
solve that question by use built in function but actually i was trying
to make this function and i want help in writing that function.

First write the skeleton

/*
mystrstr - find the first occurrence of substring in string
Params: substring - string to search for
string - string to search in.
Returns: pointer to occurence of substring in string, NULL if none found.
*/
char *mystrstr(char *substring, char *string)
{
}

Now for the algorithm.

Step through string until you find a character that matches the first
character of substring.
If you reach the end of the string, return NULL.
Now step through substring comparing every character to the following
characters in string. If you get a mismatch, abort and continue stepping
through string.
If you get to the NUL at the end of substring, you have found a perfect
match. Return the pointer to the start of the substring.

Here's a test funtion.

int main(void)
{
char *sub;

sub = mystrstr("Fred", "My name is Fred and I am dead");
/* should print out "Fred and I am dead"

ITYM
/* should print out "Fred and I am dead" */
printf("%s\n", sub);
No prototype in scope. #include <stdio.h>
sub = mystrstr("Frederick", "My name is Fred and I am dead"): ^
;
/* should print out that sub is null */
printf("%p\n", sub);
sub = mystrstr("Frederick", "Fred");
/* should print out that sub is null */
printf("%p\n", sub);
sub = mystrstr("Fred", "Fred, is Fred dead?");
/* should print out "Fred, is Fred dead?" */
printf("%s\n", sub);
sub = mystrstr("Fred", sub);
ITYM:
sub = mystrstr("Fred", sub+1);
This is the thing I originally wanted to remark but I
foundthe other stuff along the way...
/* should print out "Fred dead?");
Once again: Missing */ -- in this case an error.
printf("%s\n", sub);
return 0;
}

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 10 '06 #14
"ash" writes:
one friend advised me to use "strstr" function, this is a easy way to
solve that question by use built in function but actually i was trying
to make this function and i want help in writing that function.

Your friend is what we in America call a "smartass". It is not always a good
idea to try to show the instructor that you are more clever than he is.

You "friends" proposal answers the question: "Write a function that calls a
function that takes two strings and .....".
That is not what you were told to do.
Jun 10 '06 #15
sh***************@gmail.com wrote:
Hi
u can use strstr function which returns int telling position of first
occurance of one string in other string
by using that position(index) u can return poiter.
ash wrote:

On my system, string.h has an entry..

char * strstr(const char *_s1, const char *_s2);

...suggesting strstr does not return int.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 10 '06 #16
On Sat, 10 Jun 2006, "Malcolm" <re*******@btinternet.com> wrote:
"ash" <as************@rediffmail.com> wrote
one friend advised me to use "strstr" function, this is a easy way to
solve that question by use built in function but actually i was trying
to make this function and i want help in writing that function.

First write the skeleton

/*
mystrstr - find the first occurrence of substring in string
Params: substring - string to search for
string - string to search in.
Returns: pointer to occurence of substring in string, NULL if none found.
*/
char *mystrstr(char *substring, char *string)
{
}

Now for the algorithm.

easy if i have the right book

#include <stdio.h>
#include <stdlib.h>

#define F for
#define R return
#define W while
#define P printf
#define G goto
#define uns unsigned

// cerca in una stringa "a" la sottostringa "p"
// la stringa da cercare "p" deve essere di lunghezza
// minore di 1023 chars
// se errore: qualche stringa è il vettore nullo, oppure
// se errore di lunghezza massima di "p" (>=1023chars) ritorna 0
//
// altrimenti ritorna un puntatore alla stringa "a"
// ove ha trovato la sottostringa "p" o eventualmente
// (se non l'ha trovata) punta alla fine di "a" in &a[len]
char* kmns(char* p, char* a)
{int i,j, k;
int next[1024];
if(p==0||a==0) R 0;
F(i=0, j=-1, next[0]=-1; p[i]&&i<1023; )
{W(j>=0 && p[i]!=p[j] ) j=next[j];
++i; ++j;
next[i]=((j>=0&&p[i]==p[j])?next[j]:j);
}
if(i>=1023) R 0;
F(k=0, j=0; j<i&&a[k]; ++j, ++k)
W(j>=0 && a[k]!=p[j]) j=next[j];
R (j==i? a+(k-i): a+k);
}
// cerca in un FILE* "a" la stringa "p"
// la stringa da cercare "p" deve essere si lunghezza
// minore di 1022 chars
// se stringa non trovata ritorna 0
// se stringa trivata ritorna 1
// se errore in qualsiasi fase ritorna -1
int kmnsf(char* p, FILE* a)
{int i,j, k;
int next[1024], c;
if(p==0||a==0) R -1;
F(i=0, j=-1, next[0]=-1; p[i]&&i<1023; )
{W(j>=0 && p[i]!=p[j] ) j=next[j];
++i; ++j;
next[i]=((j>=0&&p[i]==p[j])?next[j]:j);
}
if(i>=1023) R -1;
F(j=0; j<i&&(c=getc(a))!=EOF; ++j)
W(j>=0 && c!=p[j]) j=next[j];
R (j==i? 1: 0);
}
/* Pacific Standard Time & Daylight Savings */
// char *tzstr = "TZ=PST8PDT";

int main(void)
{char *sub;

sub=kmns("Fred", "My name is Fred and i am dead");
// should print "Fred and i am dead"
P("1 sub=[%s]\n", (sub==0? "NULL":sub) );
sub=kmns("Frederick", "My name is Fred and i am dead");
P("2 sub=[%s]\n", (sub==0? "NULL":sub) );
// should print out that sub is NULL
sub = kmns("Frederick", "Fred");
P("3 sub=[%s]\n", (sub==0? "NULL":sub) );
// Null here too
sub=kmns("Fred", "Fred is Fred dead?");
// here "Fred is Fred dead"
P("4 sub=[%s]\n", (sub==0? "NULL":sub) );
// here "Fred is Fred dead"
sub=kmns("Fred", sub+1);
P("5 sub=[%s]\n", (sub==0? "NULL":sub) );
// here "Fred dead"

exit(0);
}

C:>string
1 sub=[Fred and i am dead]
2 sub=[]
3 sub=[]
4 sub=[Fred is Fred dead?]

Jun 11 '06 #17
av wrote:
.... snip ...
easy if i have the right book

#include <stdio.h>
#include <stdlib.h>

#define F for
#define R return
#define W while
#define P printf
#define G goto
#define uns unsigned

RosIwhatzis has reappeared under another name. PLONK

--
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

Jun 11 '06 #18
av wrote:
#define F for
#define R return
#define W while
#define P printf
#define G goto
#define uns unsigned

Skunk cabbage by any other name . . .

*plonk*

Brian
Jun 11 '06 #19
On Sun, 11 Jun 2006 11:08:02 +0200, av <av@ala.a> wrote:
#include <stdio.h>
#include <stdlib.h>

#define F for
#define R return
#define W while
#define P printf
#define G goto
#define uns unsigned

// cerca in una stringa "a" la sottostringa "p"
// la stringa da cercare "p" deve essere di lunghezza
// minore di 1023 chars
// se errore: qualche stringa è il vettore nullo, oppure
// se errore di lunghezza massima di "p" (>=1023chars) ritorna 0
//
// altrimenti ritorna un puntatore alla stringa "a"
// ove ha trovato la sottostringa "p" o eventualmente
// (se non l'ha trovata) punta alla fine di "a" in &a[len]
char* kmns(char* p, char* a)
{int i,j, k;
int next[1024];
if(p==0||a==0) R 0;
F(i=0, j=-1, next[0]=-1; p[i]&&i<1023; )
{W(j>=0 && p[i]!=p[j] ) j=next[j];
++i; ++j;
next[i]=((j>=0&&p[i]==p[j])?next[j]:j);
is it better here next[i]=(p[i]==p[j]?next[j]:j); ?
i say yes. j should be >0 here or not?

int kmnsf(char* p, FILE* a)
{int i,j, k;
int next[1024], c;
if(p==0||a==0) R -1;
F(i=0, j=-1, next[0]=-1; p[i]&&i<1023; )
{W(j>=0 && p[i]!=p[j] ) j=next[j];
++i; ++j;
next[i]=((j>=0&&p[i]==p[j])?next[j]:j);
the same here
}
if(i>=1023) R -1;
F(j=0; j<i&&(c=getc(a))!=EOF; ++j)

Jun 14 '06 #20

### This discussion thread is closed

Replies have been disabled for this discussion.