need some help

A

ash

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.
please suggest some advice.

2) write a 'C' function
char **readAndcreate(int n)
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.
thankx in advance
:)
 
R

Richard Heathfield

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
char **readAndcreate(int n)
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:

<http://cbfalconer.home.att.net/download/ggets.zip>

3) simply call this function in a loop, assigning each pointer thus obtained
to new, 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;
 
F

Fred Kleinschmidt

Richard Heathfield said:
ash said:


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
char **readAndcreate(int n)
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:

<http://cbfalconer.home.att.net/download/ggets.zip>

3) simply call this function in a loop, assigning each pointer thus
obtained
to new, 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)
 
O

osmium

Richard Heathfield said:
ash said:


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

Richard Heathfield

Fred Kleinschmidt said:
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.

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

ash

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

ash

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

Default User

ash said:
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
 
C

CBFalconer

Richard said:
.... 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:

<http://cbfalconer.home.att.net/download/ggets.zip>

3) simply call this function in a loop, assigning each pointer
thus obtained to new, 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)) { ... }

--
Some informative links:
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
 
R

Richard Heathfield

CBFalconer said:
Richard said:
... 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:

<http://cbfalconer.home.att.net/download/ggets.zip>

3) simply call this function in a loop, assigning each pointer
thus obtained to new, 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.
 
M

Malcolm

ash said:
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;
}
 
S

shailesh.budhaner

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

shailesh.budhaner

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

Michael Mair

Malcolm said:
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 said:
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
 
O

osmium

ash said:
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.
 
J

Joe Wright

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

av

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<1023; )
{W(j>=0 && p!=p[j] ) j=next[j];
++i; ++j;
next=((j>=0&&p==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<1023; )
{W(j>=0 && p!=p[j] ) j=next[j];
++i; ++j;
next=((j>=0&&p==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?]
5 sub=[Fred dead?]
 
C

CBFalconer

D

Default User

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
 
A

av

#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<1023; )
{W(j>=0 && p!=p[j] ) j=next[j];
++i; ++j;
next=((j>=0&&p==p[j])?next[j]:j);


is it better here next=(p==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<1023; )
{W(j>=0 && p!=p[j] ) j=next[j];
++i; ++j;
next=((j>=0&&p==p[j])?next[j]:j);


the same here
 

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,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top