473,803 Members | 3,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Missing removeStr and substr function

Hi all!

I bought the book "Programmin g 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

Nov 26 '07 #1
12 2320
On Nov 26, 1:05 pm, tfelb <tomico...@gmai l.comwrote:
Hi all!

I bought the book "Programmin g 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.

Nov 26 '07 #2
On 26 Nov., 19:13, Lew Pitcher <lpitc...@teksa vvy.comwrote:
On Nov 26, 1:05 pm, tfelb <tomico...@gmai l.comwrote:
Hi all!
I bought the book "Programmin g 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.
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[i] != '\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;
}

Nov 26 '07 #3
tfelb wrote:
Hi all!

I bought the book "Programmin g 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.n et/package/fk/0.6.7/fk-0.6.7/lib/removestr.c>
<http://www.koders.com/c/fid88DBABDF4CD0 1D6FBE11E7B5CB6 0CC2BEA98E60B.a spx>

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.
Nov 26 '07 #4
tfelb wrote:
Hi all!

I bought the book "Programmin g 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.
Did you try <mailto:st***@k ochan-wood.comas
<http://www.kochan-wood.com/AboutUs.htmsugg ests?
Nov 26 '07 #5
tfelb wrote:
Lew Pitcher <lpitc...@teksa vvy.comwrote:
>tfelb <tomico...@gmai l.comwrote:
>>I bought the book "Programmin g 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.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 26 '07 #6
On 26 Nov., 22:53, CBFalconer <cbfalco...@yah oo.comwrote:
tfelb wrote:
Lew Pitcher <lpitc...@teksa vvy.comwrote:
tfelb <tomico...@gmai l.comwrote:
>I bought the book "Programmin g 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.

--
Posted via a free Usenet account fromhttp://www.teranews.co m- Zitierten Text ausblenden -

- Zitierten Text anzeigen -
Thanks for all!
Nov 27 '07 #7
On 27 Nov., 16:08, tfelb <tomico...@gmai l.comwrote:
On 26 Nov., 22:53, CBFalconer <cbfalco...@yah oo.comwrote:


tfelb wrote:
Lew Pitcher <lpitc...@teksa vvy.comwrote:
>tfelb <tomico...@gmai l.comwrote:
>>I bought the book "Programmin g 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.
--
Posted via a free Usenet account fromhttp://www.teranews.co m-Zitierten Text ausblenden -
- 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[i] = ""; 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(strin g,"This");
printf("%s",str ing) /* the result should be "is a text" */
void removestr (char dst[], char find[])
{
int i,l;
for(i = 0, l = 0; dst[i] != '\0' && find[l] != '\0'; i++, l++)
if(dst[i] == find[l])
{
dst[i] = '\0'; /* I think here is the problem because it terminates
the whole dst */
}
dst[i] = '\0';
}

Dec 2 '07 #8
On Dec 2, 10:56 am, tfelb <tomico...@gmai l.comwrote:
On 27 Nov., 16:08, tfelb <tomico...@gmai l.comwrote:
On 26 Nov., 22:53, CBFalconer <cbfalco...@yah oo.comwrote:
tfelb wrote:
Lew Pitcher <lpitc...@teksa vvy.comwrote:
tfelb <tomico...@gmai l.comwrote:
>I bought the book "Programmin g 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.
--
Posted via a free Usenet account fromhttp://www.teranews.co m-ZitiertenText ausblenden -
- 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[i] = ""; 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(strin g,"This");
printf("%s",str ing) /* the result should be "is a text" */

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

}
dst[i] = '\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,f ind))) 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
--
Lew
Dec 3 '07 #9
On Dec 3, 10:08 am, Lew Pitcher <lpitc...@teksa vvy.comwrote:
On Dec 2, 10:56 am, tfelb <tomico...@gmai l.comwrote:
On 27 Nov., 16:08, tfelb <tomico...@gmai l.comwrote:
On 26 Nov., 22:53, CBFalconer <cbfalco...@yah oo.comwrote:
tfelb wrote:
Lew Pitcher <lpitc...@teksa vvy.comwrote:
>tfelb <tomico...@gmai l.comwrote:
>>I bought the book "Programmin g 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.
--
Posted via a free Usenet account fromhttp://www.teranews.co m-ZitiertenTextau sblenden -
- 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[i] = ""; 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(strin g,"This");
printf("%s",str ing) /* the result should be "is a text" */
void removestr (char dst[], char find[])
{
int i,l;
for(i = 0, l = 0; dst[i] != '\0' && find[l] != '\0'; i++, l++)
if(dst[i] == find[l])
{
dst[i] = '\0'; /* I think here is the problem because it terminates
the whole dst */
}
dst[i] = '\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,f ind))) 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
--
Lew
Oops... gotta fix that boundary condition, don't I? ;-)

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

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

}
Dec 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3394
by: lawrence | last post by:
I've a template with some PHP code in it. I need to get the names of all the PHP commands, so I can import them and so I can make sure they are officially allowed (for security purposes, users are only allowed to use officially allowed commands). I'm using the following class method. substr is not working as I expect. When I echo out $location1 and $location2 I get 8287 and 8306 which are correct answers for the first PHP command. But when...
1
1860
by: Patrick Londema | last post by:
Hi, I'm trying to write a function that takes a string and splits this into 3 distinct sections for a project I'm working on. This string in question has the following formet: "% 999990008312?;0008312999990000?" The important bits i want to extract are the 1st and second numbers. In this example that is 99999 and 0008312. The sequence of 9's has a fixed length of 5 chars and the other number is whatever...
2
9035
by: Daniel Mercier | last post by:
Hi, I'm new to mysql and just installed phpmyadmin. Here is the versions info: System Linux linlyne.spider.sense 2.4.22-1.2199.nptl #1 PHP Version 4.3.8
2
2909
by: lawpoop | last post by:
Hey folks - I'm working on a spellchecking routine. The pspell libraries don't handle punctuation; I have to make a script a little smarter. I'm having a problem popping English posessive endings off of strings. I thought that this little snipper would do the job: <code> if ( substr( $word, -2) == "\'s" ) {
6
4911
by: JasonFriedman80238 | last post by:
Quite suddenly, it seems, our DB does not recognize these functions: UPPER LOWER LTRIM LEAST LOG The error message is: SQL0440N No authorized routine named "LOG" of type "FUNCTION" having compatible arguments was found. SQLSTATE=42884
6
6022
by: sks | last post by:
Hi, Here is a small program that is wrtten to simply use substr. When the second parameter in substr (length of the string to be extracted) is lesser than 0, the output is the entire string (I'm using gcc 4.1.0) My questions are : 1. Is this behaviour correct ? ( Or should this case be an exception?) 2. If it is correct, what is the idea behind this behaviour ?
1
6722
by: globomike | last post by:
Hi, I am looking for an SQL function which does exactly the same like the x'<hex-string>' in an insert. Background: I want to "translate" a hexadecimal string (not number) with a length of 32 into a CHAR (16) FOR BIT DATA in a way that the hex() function of that string returns the original (hex) text.
7
4206
by: jeddiki | last post by:
Hi, I am using a function called htmlwrap() which states that it does NOT add a "<br>" to the 70 character line so that it forces a line wrap. ( the script safely wraps long words without destroying html tags which wordwrap has a tendency of doing! )) What I want to do is add that line break so that it DOES force a line wrap - but I am not sure where to insert it in the function
0
9566
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.