Missing removeStr and substr function

T

tfelb

Hi all!

I bought the book "Programming in C" by Stephen G Kochan. I miss 2
answers at his website.
(removestr and the substr function) How can I implement these
functions? It would be wonderful if someone can help me out, because
Stephen Kochan is not reachable.

Thanks for any help

T. Felb
 
L

Lew Pitcher

Hi all!

I bought the book "Programming in C" by Stephen G Kochan. I miss 2
answers at his website.
(removestr and the substr function) How can I implement these
functions? It would be wonderful if someone can help me out, because
Stephen Kochan is not reachable.


While I'm not familiar with Mr. Kochan's book or his functions, I can
guess how they might be implemented. In fact, unless Mr. Kochan has
dictated some sort of convoluted processing requirement, both
functions should be simple to the point of being obvious to anyone
with even a small amount of experience with C, character arrays, and
the definition of a string.

Why don't you post the requirements for both of these functions, and
the code you've written so far, and we'll see if we can assist you in
getting it right.
 
T

tfelb

While I'm not familiar with Mr. Kochan's book or his functions, I can
guess how they might be implemented. In fact, unless Mr. Kochan has
dictated some sort of convoluted processing requirement, both
functions should be simple to the point of being obvious to anyone
with even a small amount of experience with C, character arrays, and
the definition of a string.

Why don't you post the requirements for both of these functions, and
the code you've written so far, and we'll see if we can assist you in
getting it right.

These are the functions of Stephen G Kochan posted on his website, but
to answer my book questions I miss the substr and removestr function
to understand how these functions are implemented.

int findString (const char source[], const char s[])
{
int i, j, foundit = false;

// try each character in source

for ( i = 0; source != '\0' && !foundit; ++i ) {
foundit = true;

// now see if corresponding chars from s match

for ( j = 0; s[j] != '\0' && foundit; ++j )
if ( source[j + i] != s[j] || source[j + i] == '\0' )
foundit = false;

if (foundit)
return i;
}

return -1;
}



10-7
/* insert string s into string source starting at i
This function uses the stringLength function defined
in the chapter.

Note: this function assumes source is big enough
to store the inserted string (dangerous!) */

void insertString (char source[], char s[], int i)
{
int j, lenS, lenSource;

/* first, find out how big the two strings are */

lenSource = stringLength (source);
lenS = stringLength (s);

/* sanity check here -- note that i == lenSource
effectively concatenates s onto the end of source */

if (i > lenSource)
return;

/* now we have to move the characters in source
down from the insertion point to make room for s.
Note that we copy the string starting from the end
to avoid overwriting characters in source.
We also copy the terminating null (j starts at lenS)
as well since the final result must be null-terminated */

for ( j = lenSource; j >= i; --j )
source [lenS + j] = source [j];

/* we've made room, now copy s into source at the
insertion point */

for ( j = 0; j < lenS; ++j )
source [j + i] = s[j];
}



10-9
bool replaceString (char source [], char s1[], char s2[])
{
int index;

// first locate s1 inside the source

index = findString (source, s1);

if ( index == -1 )
return false;

// now delete s1 from the source

removeString (source, index, stringLength (s1));

// now insert the new string

insertString (source, s2, index);

return true;
}
 
M

Martin Ambuhl

tfelb said:
Hi all!

I bought the book "Programming in C" by Stephen G Kochan. I miss 2
answers at his website.
(removestr and the substr function) How can I implement these
functions? It would be wonderful if someone can help me out, because
Stephen Kochan is not reachable.

A simple Google search yields
<http://csourcesearch.net/package/fk/0.6.7/fk-0.6.7/lib/removestr.c>
<http://www.koders.com/c/fid88DBABDF4CD01D6FBE11E7B5CB60CC2BEA98E60B.aspx>

I have made no effort to check the quality of the code.
In any case, comp.lang.c is not a sources-wanted newsgroup, and there
_are_ such newsgroups. Try one.
 
L

Lew Pitcher

While I'm not familiar with Mr. Kochan's book or his functions, I can
guess how they might be implemented. In fact, unless Mr. Kochan has
dictated some sort of convoluted processing requirement, both
functions should be simple to the point of being obvious to anyone
with even a small amount of experience with C, character arrays, and
the definition of a string.
Why don't you post the requirements for both of these functions, and
the code you've written so far, and we'll see if we can assist you in
getting it right.

These are the functions of Stephen G Kochan posted on his website, but
to answer my book questions I miss the substr and removestr function
to understand how these functions are implemented.

int findString (const char source[], const char s[])
{
int i, j, foundit = false;

// try each character in source

for ( i = 0; source != '\0' && !foundit; ++i ) {
foundit = true;

// now see if corresponding chars from s match

for ( j = 0; s[j] != '\0' && foundit; ++j )
if ( source[j + i] != s[j] || source[j + i] == '\0' )
foundit = false;

if (foundit)
return i;
}

return -1;

}

[snip]

Well, it appears that findString() is a workable substitute for the
missing substr() function. So, one down and one to go.

As I said, it is trivially easy to write these two functions. Now that
you have substr() (or it's equivalent), think of how you would delete
a substring from a string. Here's how I would do it: I'd start moving
characters from beyond the right end of the substring to the left
positions of the substring, until I run out of string.

take from here
! A ! B ! C ! D ! E ! F ! G ! \0 !
^ :
'-----------'
and move to here, until you move the \0

You are left with...

! A ! E ! F ! G ! \0 ! ! ! !

thus deleting the substring "BCD" from the string "ABCDEFG"

Now, go code that program
 
C

CBFalconer

tfelb said:
Lew Pitcher said:
.... snip ...

Why don't you post the requirements for both of these functions,
and the code you've written so far, and we'll see if we can
assist you in getting it right.

These are the functions of Stephen G Kochan posted on his website,
but to answer my book questions I miss the substr and removestr
function to understand how these functions are implemented.

int findString (const char source[], const char s[])
.... snip much code ...

You posted all that but didn't bother to answer Lews question.
Describe, in detail, what those functions (substr and removestr)
do. Include a complete prototype.
 
T

tfelb

... snip ...
These are the functions of Stephen G Kochan posted on his website,
but to answer my book questions I miss the substr and removestr
function to understand how these functions are implemented.
int findString (const char source[], const char s[])

... snip much code ...

You posted all that but didn't bother to answer Lews question.
Describe, in detail, what those functions (substr and removestr)
do. Include a complete prototype.

Thanks for all!
 
T

tfelb

... snip ...
Why don't you post the requirements for both of these functions,
and the code you've written so far, and we'll see if we can
assist you in getting it right.
These are the functions of Stephen G Kochan posted on his website,
but to answer my book questions I miss the substr and removestr
function to understand how these functions are implemented.
int findString (const char source[], const char s[])
... snip much code ...
You posted all that but didn't bother to answer Lews question.
Describe, in detail, what those functions (substr and removestr)
do. Include a complete prototype.
- Zitierten Text anzeigen -

Thanks for all!- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

I coded the substring function and it works fine but I have problems
with the removestr function. I want to code this function without any
pointers. I'm not sure how can I delete a char in C because '\0' is
the end of each string and it terminates the string. I tried to set
dst = ""; but then I got an errormessage "warning: assignment makes
integer from pointer without a cast", so "" doesn't work.

Thanks for any help!

Tom

Prototyp: void removestr(char [], char [])

Use:

char string[100] = "This is a text\n";
removestr(string,"This");
printf("%s",string) /* the result should be "is a text" */


void removestr (char dst[], char find[])
{
int i,l;
for(i = 0, l = 0; dst != '\0' && find[l] != '\0'; i++, l++)
if(dst == find[l])
{
dst = '\0'; /* I think here is the problem because it terminates
the whole dst */
}
dst = '\0';
}
 
L

Lew Pitcher

tfelb wrote:
I bought the book "Programming in C" by Stephen G Kochan. I miss
2 answers at his website. (removestr and the substr function)
How can I implement these functions? It would be wonderful if
someone can help me out, because Stephen Kochan is not reachable.
... snip ...
Why don't you post the requirements for both of these functions,
and the code you've written so far, and we'll see if we can
assist you in getting it right.
These are the functions of Stephen G Kochan posted on his website,
but to answer my book questions I miss the substr and removestr
function to understand how these functions are implemented.
int findString (const char source[], const char s[])
... snip much code ...
You posted all that but didn't bother to answer Lews question.
Describe, in detail, what those functions (substr and removestr)
do. Include a complete prototype.
Thanks for all!- Zitierten Text ausblenden -
- Zitierten Text anzeigen -

I coded the substring function and it works fine but I have problems
with the removestr function. I want to code this function without any
pointers. I'm not sure how can I delete a char in C because '\0' is
the end of each string and it terminates the string. I tried to set
dst = ""; but then I got an errormessage "warning: assignment makes
integer from pointer without a cast", so "" doesn't work.

Thanks for any help!

Tom

Prototyp: void removestr(char [], char [])

Use:

char string[100] = "This is a text\n";
removestr(string,"This");
printf("%s",string) /* the result should be "is a text" */

void removestr (char dst[], char find[])
{
int i,l;
for(i = 0, l = 0; dst != '\0' && find[l] != '\0'; i++, l++)
if(dst == find[l])
{
dst = '\0'; /* I think here is the problem because it terminates
the whole dst */

}
dst = '\0';
}



OK, well you really missed the idea, didn't you? ;-)

I'm not even going to attempt to help fix your immediate problem in
this code, as this code does not now (and likely never will) answer
the requirement of deleting an arbitrary substring from an arbitrary
string.

So, lets start at the beginning; here's how I would delete an
arbitrary substring from an arbitrary string:

1) locate and note the beginning of the substring in the string - If
not found then exit
2) locate and note the first character following the substring in the
string
3) for each character in the substring,
copy the character following the substring (point 2) over the
character in the substring (point 1)
move forward one character in the substring (point 1)
move forward one character following the substring (point 2)


void removestr(char *dst, char *find)
{
char *start, *end;
if (!(start = end = substring(dst,find))) return; /* exit if the
substring cant be found */

while (*find) {++find; ++end;} /* advance
past substring */
while (*end) {*start = *end; ++start; ++end;} /* copy
remainder over substring */
}

HTH
 
L

Lew Pitcher

tfelb wrote:
I bought the book "Programming in C" by Stephen G Kochan. I miss
2 answers at his website. (removestr and the substr function)
How can I implement these functions? It would be wonderful if
someone can help me out, because Stephen Kochan is not reachable.
... snip ...
Why don't you post the requirements for both of these functions,
and the code you've written so far, and we'll see if we can
assist you in getting it right.
These are the functions of Stephen G Kochan posted on his website,
but to answer my book questions I miss the substr and removestr
function to understand how these functions are implemented.
int findString (const char source[], const char s[])
... snip much code ...
You posted all that but didn't bother to answer Lews question.
Describe, in detail, what those functions (substr and removestr)
do. Include a complete prototype.
--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.
--
-ZitiertenTextausblenden -
- Zitierten Text anzeigen -
Thanks for all!- Zitierten Text ausblenden -
- Zitierten Text anzeigen -
I coded the substring function and it works fine but I have problems
with the removestr function. I want to code this function without any
pointers. I'm not sure how can I delete a char in C because '\0' is
the end of each string and it terminates the string. I tried to set
dst = ""; but then I got an errormessage "warning: assignment makes
integer from pointer without a cast", so "" doesn't work.

Thanks for any help!

Prototyp: void removestr(char [], char [])

char string[100] = "This is a text\n";
removestr(string,"This");
printf("%s",string) /* the result should be "is a text" */
void removestr (char dst[], char find[])
{
int i,l;
for(i = 0, l = 0; dst != '\0' && find[l] != '\0'; i++, l++)
if(dst == find[l])
{
dst = '\0'; /* I think here is the problem because it terminates
the whole dst */

}
dst = '\0';
}


OK, well you really missed the idea, didn't you? ;-)

I'm not even going to attempt to help fix your immediate problem in
this code, as this code does not now (and likely never will) answer
the requirement of deleting an arbitrary substring from an arbitrary
string.

So, lets start at the beginning; here's how I would delete an
arbitrary substring from an arbitrary string:

1) locate and note the beginning of the substring in the string - If
not found then exit
2) locate and note the first character following the substring in the
string
3) for each character in the substring,
copy the character following the substring (point 2) over the
character in the substring (point 1)
move forward one character in the substring (point 1)
move forward one character following the substring (point 2)

void removestr(char *dst, char *find)
{
char *start, *end;
if (!(start = end = substring(dst,find))) return; /* exit if the
substring cant be found */

while (*find) {++find; ++end;} /* advance
past substring */
while (*end) {*start = *end; ++start; ++end;} /* copy
remainder over substring */

}

HTH


Oops... gotta fix that boundary condition, don't I? ;-)

void removestr(char *dst, char *find)
{
char *start, *end;
if (!(start = end = substring(dst,find)))
return; /* exit if cant find substring */

while (*find)
{++find; ++end;} /* advance past substring */
while (*start = *end)
{++start; ++end;} /* copy remainder over substring */

}
 
T

tfelb

tfelb wrote:
I bought the book "Programming in C" by Stephen G Kochan. I miss
2 answers at his website. (removestr and the substr function)
How can I implement these functions? It would be wonderful if
someone can help me out, because Stephen Kochan is not reachable.
... snip ...
Why don't you post the requirements for both of these functions,
and the code you've written so far, and we'll see if we can
assist you in getting it right.
These are the functions of Stephen G Kochan posted on his website,
but to answer my book questions I miss the substr and removestr
function to understand how these functions are implemented.
int findString (const char source[], const char s[])
... snip much code ...
You posted all that but didn't bother to answer Lews question.
Describe, in detail, what those functions (substr and removestr)
do. Include a complete prototype.
--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.
--
-ZitiertenTextausblenden-
- Zitierten Text anzeigen -
Thanks for all!- Zitierten Text ausblenden -
- Zitierten Text anzeigen -
I coded the substring function and it works fine but I have problems
with the removestr function. I want to code this function without any
pointers. I'm not sure how can I delete a char in C because '\0' is
the end of each string and it terminates the string. I tried to set
dst = ""; but then I got an errormessage "warning: assignment makes
integer from pointer without a cast", so "" doesn't work.
Thanks for any help!
Tom
Prototyp: void removestr(char [], char [])
Use:
char string[100] = "This is a text\n";
removestr(string,"This");
printf("%s",string) /* the result should be "is a text" */
void removestr (char dst[], char find[])
{
int i,l;
for(i = 0, l = 0; dst != '\0' && find[l] != '\0'; i++, l++)
if(dst == find[l])
{
dst = '\0'; /* I think here is the problem because it terminates
the whole dst */
}
dst = '\0';
}

OK, well you really missed the idea, didn't you? ;-)
I'm not even going to attempt to help fix your immediate problem in
this code, as this code does not now (and likely never will) answer
the requirement of deleting an arbitrary substring from an arbitrary
string.
So, lets start at the beginning; here's how I would delete an
arbitrary substring from an arbitrary string:
1) locate and note the beginning of the substring in the string - If
not found then exit
2) locate and note the first character following the substring in the
string
3) for each character in the substring,
copy the character following the substring (point 2) over the
character in the substring (point 1)
move forward one character in the substring (point 1)
move forward one character following the substring (point 2)
void removestr(char *dst, char *find)
{
char *start, *end;
if (!(start = end = substring(dst,find))) return; /* exit if the
substring cant be found */
while (*find) {++find; ++end;} /* advance
past substring */
while (*end) {*start = *end; ++start; ++end;} /* copy
remainder over substring */

HTH

Oops... gotta fix that boundary condition, don't I? ;-)

void removestr(char *dst, char *find)
{
char *start, *end;
if (!(start = end = substring(dst,find)))
return; /* exit if cant find substring */

while (*find)
{++find; ++end;} /* advance past substring */
while (*start = *end)
{++start; ++end;} /* copy remainder over substring */



}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -


Oh THANK YOU very much! Overwriting is the solution.

Tom
 
B

Barry Schwarz

snip
I coded the substring function and it works fine but I have problems
with the removestr function. I want to code this function without any
pointers. I'm not sure how can I delete a char in C because '\0' is
the end of each string and it terminates the string. I tried to set
dst = ""; but then I got an errormessage "warning: assignment makes
integer from pointer without a cast", so "" doesn't work.

Thanks for any help!

Tom

Prototyp: void removestr(char [], char [])

Use:

char string[100] = "This is a text\n";
removestr(string,"This");


There is no space after the This, so
printf("%s",string) /* the result should be "is a text" */

the result should be " is a text\n".
void removestr (char dst[], char find[])

You say you don't want to use pointers. Are you aware that when an
array is passed into a function, the array argument is converted to a
pointer to the first element. Your calling statement is identical to
removestr(&string[0], "This");
If it were legal syntax, "This" could be replaced with &"This"[0].
{
int i,l;
for(i = 0, l = 0; dst != '\0' && find[l] != '\0'; i++, l++)
if(dst == find[l])
{
dst = '\0'; /* I think here is the problem because it terminates
the whole dst */
}


If dst[0] != find[0], you increment both i and l. Your next iteration
through the loop will test dst[1] and find[1] but you should be
checking dst[1] and find[0].
dst = '\0';
}


Consider the more general case of removing the b from abc. You want
the answer to be ac. If your design does not move the a (there is no
need to), then you must move the c and any subsequent characters
including the terminating '\0' over to where the b was. Think of the
one function that will allow you to move data where the source and
destination overlap.


Remove del for email
 
H

Harald van Dijk

void removestr (char dst[], char find[])

You say you don't want to use pointers. Are you aware that when an
array is passed into a function, the array argument is converted to a
pointer to the first element. Your calling statement is identical to
removestr(&string[0], "This");
If it were legal syntax, "This" could be replaced with &"This"[0].

What's invalid about &"This"[0]?
 

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
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top