This function str_join()
is an updated version of my earlier function strcat_new()
— found here: strcat_new()
function, not present in the Standard C library.
The older version strcat_new()
had a bug that is fixed in this version of str_join()
.
Syntax: char *str_join(const char *delim, long num_args, ...);
str_join.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "str_join.h"
/*
* str_join:
*
* Parameters:
* num_args: number of variable arguments that are passed to this function
* excluding the 'delim' string.
* ...: Variable number of pointers to character arrays.
*
* Description:
* str_join concatenates all the strings/character arrays passed to it. If
* 'delim' is not NULL then after every string, the 'delim' string is concatenated.
* It allocates a new character array whose size is equal to the sum of the
* lengths of all strings passed to it plus 1 (extra 1 for terminating null byte).
* It then concatenates all the strings passed to it separated by 'delim' string
* into the newly allocated character array and then returns the pointer to
* the newly allocated character array. If memory allocation fails then NULL is returned.
*
* It is the responsibility of the caller to free the allocated memory.
*/
char *str_join(const char *delim, long num_args, ...)
{
va_list valist;
long i = 0;
long j = 0;
long iica = 0; // iica - index into character array
long len = 0;
long delim_len = 0;
long total_len = 0;
long num_delim_to_concat = -1;
char *new_char_array = NULL;
char *temp = NULL;
if (num_args <= 0)
return NULL;
if (delim) {
delim_len = strlen(delim);
}
va_start(valist, num_args);
for (i = 0; i < num_args; i++) {
temp = va_arg(valist, char *);
if (!temp) continue;
total_len = total_len + strlen(temp);
num_delim_to_concat = num_delim_to_concat + 1;
}
va_end(valist);
if (total_len == 0)
return NULL;
total_len = total_len + (num_delim_to_concat) * (delim_len);
total_len = total_len + 1; // 1 extra for terminating null byte
new_char_array = malloc(total_len);
if (!new_char_array)
return NULL;
va_start(valist, num_args);
for (i = 0; i < num_args; i++) {
temp = va_arg(valist, char *);
if (!temp) continue;
len = strlen(temp);
for (j = 0; j < len; j++) {
new_char_array[iica] = temp[j];
iica++;
}
if (num_delim_to_concat > 0) {
for (j = 0; j < delim_len; j++) {
new_char_array[iica] = delim[j];
iica++;
}
num_delim_to_concat = num_delim_to_concat - 1;
}
}
va_end(valist);
new_char_array[iica] = 0;
return new_char_array;
} // end of str_join
str_join.h
#ifndef _STRCAT_NEW_H_
#define _STRCAT_NEW_H_
/*
* str_join:
*
* Parameters:
* num_args: number of variable arguments that are passed to this function
* excluding the 'delim' string.
* ...: Variable number of pointers to character arrays.
*
* Description:
* str_join concatenates all the strings/character arrays passed to it. If
* 'delim' is not NULL then after every string, the 'delim' string is concatenated.
* It allocates a new character array whose size is equal to the sum of the
* lengths of all strings passed to it plus 1 (extra 1 for terminating null byte).
* It then concatenates all the strings passed to it separated by 'delim' string
* into the newly allocated character array and then returns the pointer to
* the newly allocated character array. If memory allocation fails then NULL is returned.
*
* It is the responsibility of the caller to free the allocated memory.
*/
char *str_join(const char *delim, long num_args, ...);
#endif
test_str_join.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "str_join.h"
int main(void)
{
char *a = str_join(";:?", 4, "abc", "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = str_join(NULL, 3, "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = str_join(NULL, 0, "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):(unsigned long)(-1));
free(a);
a = str_join(NULL, -1, "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):(unsigned long)(-1));
free(a);
a = str_join("=", 4, "abc", "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = str_join("{(=%$^%^&&(&)}", 4, "abc", "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = str_join(";;;", 4, "abc", NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = str_join(";;;", 4, "abc", NULL, NULL, "cde");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = str_join(NULL, 4, NULL, "fdr", NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):(unsigned long)(-1));
free(a);
a = str_join("-------", 10, "START", "hfjkdhf78578695590", "nvmcnkjfhgkjfh^&*^&*", "7856475947584", "!@^&^%#*&(*$&(&$(*", "j", "4", "khfkjhgiury6985768956uirhtukrhgkhfk", "jfjlkjgk65876895769576895khgkjfkkhkjhjG%^&%#*@^*@^$*&^#&$*#$#*$&#$&$(*#", "END");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):(unsigned long)(-1));
free(a);
a = str_join(NULL, 1, NULL, "fdr", NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):(unsigned long)(-1));
free(a);
a = str_join(";", 4, NULL, NULL, NULL, "xyz");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):(unsigned long)(-1));
free(a);
a = str_join(";;;", 4, NULL, NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):(unsigned long)(-1));
free(a);
a = str_join(NULL, 4, NULL, NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):(unsigned long)(-1));
free(a);
printf("\n");
}
return 0;
inmain()
. \$\endgroup\$