I am wondering if anyone could help me, I am trying to concatenate 3 strings and return a pointer to the new string. I can't seem to figure out how to do this using strncat
instead of strcat
and strncpy
instead of strcpy
. I am only learning C, so any help will be greatly appreciated.
char *concatenate(char *a, char *b, char *d) {
char str[80];
strcpy(str, a);
strcat(str, b);
strcat(str, d);
puts(str);
return (NULL);
}
C-strings use the strcat() function for concatenation.
In C, the strcat() function is used to concatenate two strings. It concatenates one string (the source) to the end of another string (the destination). The pointer of the source string is appended to the end of the destination string, thus concatenating both strings.
You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.
Your approach cannot be used to return the concatenated string: you would return a pointer to a local array that is no longer valid once the function returns, furthermore, you do not check for buffer overflow.
Here is a quick and dirty version that allocates memory:
#include <stdlib.h>
#include <string.h>
char *concatenate(const char *a, const char *b, const char *c) {
return strcat(strcat(strcpy(malloc(strlen(a) + strlen(b) + strlen(c) + 1,
a), b), c);
}
Here is a more elaborate version using memcpy
and testing for malloc
failure:
#include <stdlib.h>
#include <string.h>
char *concatenate(const char *a, const char *b, const char *c) {
size_t alen = strlen(a);
size_t blen = strlen(b);
size_t clen = strlen(c);
char *res = malloc(alen + blen + clen + 1);
if (res) {
memcpy(res, a, alen);
memcpy(res + alen, b, blen);
memcpy(res + alen + blen, c, clen + 1);
}
return res;
}
It should be more efficient since it does not perform the extra scans strcpy
and strcat
do, but only careful benchmarking can prove if it is a real improvement over the simple version above.
If you need to concatenate 3 strings into an existing buffer, a very simple solution is:
char dest[DEST_SIZE];
snprintf(dest, sizeof dest, "%s%s%s", a, b, c);
Many systems (linux, GNU, BSD) have an asprintf()
function defined that allocates memory for the resulting string:
int asprintf(char **strp, const char *fmt, ...);
Using this function, you can concatenate the three strings quite simply:
#define _GNU_SOURCE
#include <stdio.h>
char *concatenate(const char *a, const char *b, const char *c) {
char *p;
return (asprintf(&p, "%s%s%s", a, b, c) >= 0) ? p : NULL;
}
Your str is local to your function.
You could add a fourth parameter to your concatenated string or you could malloc it inside the function, just make sure to free it after use.
char *concatenate(char *a, char *b, char *c)
{
int size = strlen(a) + strlen(b) + strlen(c) + 1;
char *str = malloc(size);
strcpy (str, a);
strcat (str, b);
strcat (str, c);
return str;
}
int main(void) {
char *str = concatenate("bla", "ble", "bli");
printf("%s", str);
free(str);
return 0;
}
Maybe something like that:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * concatenate(const char *a, const char *b, const char *d)
{
/* calculate the length of the new string */
size_t len = strlen(a) + strlen(b) + strlen(d);
/* allocate memory for the new string */
char* str = malloc(len + 1);
/* concatenate */
strcpy(str, a);
strcat(str, b);
strcat(str, d);
/* return the pointer to the new string
* NOTE: clients are responsible for releasing the allocated memory
*/
return str;
}
int main(void)
{
const char a[] = "lorem";
const char b[] = "impsum";
const char d[] = "dolor";
char* str = concatenate(a, b, d);
printf("%s\n", str);
free(str);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With