By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,542 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

String concat using Recursion

P: n/a
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

Jan 23 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a

Raman wrote:
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.

Jan 23 '07 #2

P: n/a
On 22 Jan 2007 21:31:18 -0800, "Raman" <ra***********@gmail.com>
wrote:
>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
--
jay
Jan 23 '07 #3

P: n/a
jaysome wrote:
On 22 Jan 2007 21:31:18 -0800, "Raman" <ra***********@gmail.com>
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.
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)

Jan 23 '07 #4

P: n/a
On 23 Jan 2007 01:38:15 -0800, "Nick Keighley"
<ni******************@hotmail.comwrote:
>jaysome wrote:
>On 22 Jan 2007 21:31:18 -0800, "Raman" <ra***********@gmail.com>
>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.

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]
>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?
Yes.

Regards,
--
jay
Jan 23 '07 #5

P: n/a

jaysome wrote:
On 23 Jan 2007 01:38:15 -0800, "Nick Keighley"
<ni******************@hotmail.comwrote:
jaysome wrote:
On 22 Jan 2007 21:31:18 -0800, "Raman" <ra***********@gmail.com>
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.
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;
}
Did you check the code ?? did you get a core dump ??
>
[snip]
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?

Yes.

Regards,
--
jay
Jan 23 '07 #6

P: n/a
"Raman" wrote:
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.
Jan 23 '07 #7

P: n/a
>>>>"j" == jaysome <ja*****@hotmail.comwrites:

jOn 22 Jan 2007 21:31:18 -0800, "Raman"
j<ra***********@gmail.com>
jwrote:
>Could any one tell me How to concatenate two strings using
recursion.
jWhy would you ever want to do that instead of using the
jstandard C function strcat()? I'll tell you why--never.

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

He probably is -- "do <taskrecursively," where <taskis 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
--
Charlton Wilbur
cw*****@chromatico.net
Jan 23 '07 #8

P: n/a
Nick Keighley wrote:
jaysome wrote:
>On 22 Jan 2007 21:31:18 -0800, "Raman" <ra***********@gmail.com>
>>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.

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
Jan 23 '07 #9

P: n/a
On 23 Jan 2007 02:53:22 -0800, "yeti" <ro********@gmail.comwrote:
>
jaysome wrote:
>On 23 Jan 2007 01:38:15 -0800, "Nick Keighley"
<ni******************@hotmail.comwrote:
>jaysome wrote:
On 22 Jan 2007 21:31:18 -0800, "Raman" <ra***********@gmail.com>

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.

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;
}
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
--
jay
Jan 24 '07 #10

P: n/a


On 23 Jan, 17:31, CBFalconer <cbfalco...@yahoo.comwrote:
Nick Keighleywrote:
jaysome wrote:
On 22 Jan 2007 21:31:18 -0800, "Raman" <ramanchalo...@gmail.com>
>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.
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.
I was unclear. I meant "sometimes there is a reason to write strcat()"
*not* "there
is a reason to write a recursive strcat()".
--
Nick Keighley

Jan 24 '07 #11

P: n/a
jaysome wrote:
"yeti" <ro********@gmail.comwrote:
>jaysome wrote:
>><ni******************@hotmail.comwrote:
jaysome wrote:
"Raman" <ra***********@gmail.com>

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

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;
}
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++);
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
Jan 24 '07 #12

P: n/a
CBFalconer wrote:
Nick Keighley wrote:
>>jaysome wrote:
>>>On 22 Jan 2007 21:31:18 -0800, "Raman" <ra***********@gmail.com>

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.

embedded systems where the standard library isn't available?


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
mf*****@sbcglobal.net
Jan 24 '07 #13

P: n/a
CBFalconer <cb********@yahoo.comwrote:
jaysome wrote:
"yeti" <ro********@gmail.comwrote:
jaysome wrote:
char *strcat
(
char *s1,
const char *s2
)
{
char *p = s1;
while(*p) p++;
while(*p++ = *s2);
return s1;
}
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++);

strlcpy/strlcat have a much better interface.
Yes, jacob, but they're not the functions under discussion here.

Richard
Jan 24 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.