suggestion for writing a program to concatanate two strings

A

Artie Gold

Simon said:
Artie said:
vire wrote:

[failing to quote the relevant portions of the post to which he/she
was responding]
#include <stdio.h>
#include <string.h>

char* getCat(char* str1,char* str2)
{
int i=strlen(str1)+strlen(str2);



BZZZZT! And the terminating null character goes where?
ITYM:
int i = strlen(str1) + strlen(str2);


I fail to see any meaningful difference. I think you meant to add a one

Ack! Of course!!!!!
in there. Also, the variable should be of type size_t.



That will require that you #include <stdlib.h>, which I didn't see you
mention.
....and some nights I *should* go to bed early...

;-(
--ag
 
M

Mark McIntyre

Thank you.
I want to write code with out using string handling functions.
So i wrote the code like that.
Now i just want to return it to tha main function.
once again thanks
 
P

pete

I want to write code with out using string handling functions.

/* BEGIN new.c */

#include <stdio.h>

char *string_concatenate(char *s1, char *s2);
char *str_cpy(char *s1, const char *s2);
size_t str_len(const char *s);

int main(void)
{
char s1[sizeof "concatenate string"] = "concatenate ";
char s2[] = "string";
char *p;

p = string_concatenate(s1, s2);
puts(p);
return 0;
}

char *string_concatenate(char *s1, char *s2)
{
str_cpy(str_len(s1) + s1, s2);
return s1;
}

size_t str_len(const char *s)
{
size_t n;

for (n = 0; *s != '\0'; ++s) {
++n;
}
return n;
}

char *str_cpy(char *s1, const char *s2)
{
char *const p1 = s1;

do {
*s1++ = *s2;
} while (*s2++ != '\0');
return p1;
}

/* END new.c */
 
M

Michael Mair

pete said:
/* BEGIN new.c */

#include <stdio.h>

char *string_concatenate(char *s1, char *s2);
^
As I am sure you know the gospel, I am just curious: Why?

<snip: code at usual pete quality>

Cheers
Michael
 

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,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top