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 wil. be greatly aprecaited.
char *
concatenate(char *a, char *b, char *d)
{
char str[80];
strcpy (str, a);
strcat (str,b);
strcat (str,d);
puts (str);
return (NULL);
}
You cannot do it this way, 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);
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;
}
如果您想要更通用的东西(例如连接 N 个字符串),您可以在此处查看 glib 库的 g_strconcat 的实现:https ://github.com/GNOME/glib/blob/master/glib/gstrfuncs。 c#L563
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.