String concat using Recursion

R

Raman

Hi All,

Could any one tell me How to concatenate two strings using recursion.
And also how to trim a string using recursion.(in C, offcourse)

Regards,
Raman Chalotra
 
C

Chris Johnson

Raman said:
Hi All,

Could any one tell me How to concatenate two strings using recursion.
And also how to trim a string using recursion.(in C, offcourse)

I would recommend doing your own homework.

But I'll give you a hint. You would normally need to iterate to perform
these functions, correct? Generally, you'll pass the data structure you
iterate (over) as an argument, when translating to a recursive function.
 
J

jaysome

Hi All,

Could any one tell me How to concatenate two strings using recursion.

Why would you ever want to do that instead of using the standard C
function strcat()? I'll tell you why--never.

If this is a question posed by an instructor, then said instructor is
wasting your time. He or she should be focusing on the problems
related to what you should be doing instead of anything related to
what you shouldn't be doing.

I'd ask the instructor my 10 favorite standard C quesdtions, and I'd
bet he or she wouldn't get all 10 correct. I warn you, don't call my
bluff :^)
And also how to trim a string using recursion.(in C, offcourse)

Again, why?

Regardless of why, the word "trim" is too vague to answer your
question. In the following program, I "trim" a string, but your idea
of "trim" most likely differs from mine.

#include <stdio.h>
#define S "Hello the poor world--and the rich!"
int main(void)
{
char s[sizeof S] = S;
printf("Before trim: %s\n", s);
s[20] = '\0';/*trim*/
printf("After trim: %s\n", s);
return 0;
}

Regards
 
N

Nick Keighley

jaysome said:
On 22 Jan 2007 21:31:18 -0800, "Raman" <[email protected]>

Why would you ever want to do that instead of using the standard C
function strcat()? I'll tell you why--never.

embedded systems where the standard library isn't available?

If this is a question posed by an instructor, then said instructor is
wasting your time. He or she should be focusing on the problems
related to what you should be doing instead of anything related to
what you shouldn't be doing.

not always a waste of time. Knowing how to manipulate strings might
help when you get a harder problem By your criteria "hello world" is
a waste of time.

I'd ask the instructor my 10 favorite standard C quesdtions, and I'd
bet he or she wouldn't get all 10 correct. I warn you, don't call my
bluff :^)

could we see, please?


<snip>


--
Nick Keighley
Infinitely many bits doesn't give you "100% accuracy". You will
only be able to represent the algebraic numbers.

Sure, if you use one of those old-fashioned implementations that only
has aleph-null-bit floating-point. Any decent modern implementation
should provide at least aleph-one bits.
(Bill Pursell and Keith Thompson clc)
 
J

jaysome

embedded systems where the standard library isn't available?

Why?

It's straight forward to roll your own, without using recursion.

char *strcat
(
char *s1,
const char *s2
)
{
char *p = s1;
while(*p) p++;
while(*p++ = *s2);
return s1;
}

[snip]
could we see, please?

Yes.

Regards,
 
O

osmium

Raman said:
Could any one tell me How to concatenate two strings using recursion.

Getting your head around recursion can be tough. In this case, it might be
helpful to think of what the terminal condition will be. There will be a
function that eventually returns because there is nothing left to add to the
longer string. So the function call would likely be for a function with two
arguments, return_part and add_part: It returns, unwinds, instead of
making another recursive call, because there is nothing more left in
add_part to add on to the return_part. Converting the thoughts to code,
results in something like this:

void concat(char* return_part, char* add_part)

Now write the code.I caution you that I didn't try to pursue this to the
bitter end - working code. For a student problem it is probably permissible
to ignore error conditions. For example, the space allocated to return_part
is too small.
 
C

Charlton Wilbur

j> On 22 Jan 2007 21:31:18 -0800, "Raman"

j> Why would you ever want to do that instead of using the
j> standard C function strcat()? I'll tell you why--never.

j> If this is a question posed by an instructor, then said
j> instructor is wasting your time. He or she should be focusing
j> on the problems related to what you should be doing instead of
j> anything related to what you shouldn't be doing.

He probably is -- "do <task> recursively," where <task> is a simple,
well-defined problem, is one of the best ways to make sure that the
student grasps recursion.

Sure, there's a library function to do it. That's not the point.

Charlton
 
C

CBFalconer

Nick said:
embedded systems where the standard library isn't available?

Simply embed my published source code for strlcat, for example.
Works every time. Resource use is predictable, unlike a recursive
solution.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
J

jaysome

Did you check the code ?? did you get a core dump ??

Whooooops!

The testing department was closed when I submitted that, so it was
never tested. But of course you're correct about the bug. That should
read:

while(*p++ = *s2++);

Thanks
 
N

Nick Keighley

Nick Keighleywrote:

Works every time. Resource use is predictable, unlike a recursive
solution.

I was unclear. I meant "sometimes there is a reason to write strcat()"
*not* "there
is a reason to write a recursive strcat()".
 
C

CBFalconer

jaysome said:
Whooooops!

The testing department was closed when I submitted that, so it was
never tested. But of course you're correct about the bug. That
should read:

while(*p++ = *s2++);

strlcpy/strlcat have a much better interface. An extract:

#include "strlcpy.h"

/* NOTE: these routines are deliberately designed to
not require any assistance from the standard
libraries. This makes them more useful in any
embedded systems that must minimize the load size.

Public domain, by C.B. Falconer
bug reports to mailto:cbfalconer@see elsewhere
*/

/* ---------------------- */

size_t strlcpy(char *dst, const char *src, size_t sz)
{
const char *start = src;

if (src && sz--) {
while ((*dst++ = *src))
if (sz--) src++;
else {
*(--dst) = '\0';
break;
}
}
if (src) {
while (*src++) continue;
return src - start - 1;
}
else if (sz) *dst = '\0';
return 0;
} /* strlcpy */

/* ---------------------- */

size_t strlcat(char *dst, const char *src, size_t sz)
{
char *start = dst;

while (*dst++) /* assumes sz >= strlen(dst) */
if (sz) sz--; /* i.e. well formed string */
dst--;
return dst - start + strlcpy(dst, src, sz);
} /* strlcat */

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
M

Mark F. Haigh

CBFalconer said:
Simply embed my published source code for strlcat, for example.
<snip>

I could have swore that ISO/IEC 9899:1999 Section 7.26.11 explicitly
reserves function names that begin with "str" followed by a lowercase
letter.
> Works every time.

Hmm. Is that so?


Mark F. Haigh
(e-mail address removed)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top