strcat_new()
function, not present in standard C library.
Syntax: char *strcat_new(char *delim, long num_args, ...);
The code is below. Can someone please do the code review?
UPDATE:
This version of strcat_new() has a bug. This bug has been fixed in the newer version. The newer version is called str_join(). You can find the newer version here: str_join() function, not present in standard C library
strcat_new.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "strcat_new.h"
/*
* strcat_new:
*
* 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:
* strcat_new 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 *strcat_new(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;
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) + delim_len;
}
va_end(valist);
total_len = total_len - delim_len; // remove the last delimiter
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 (i < (num_args - 1)) {
for (j = 0; j < delim_len; j++) {
new_char_array[iica] = delim[j];
iica++;
}
}
}
va_end(valist);
new_char_array[iica] = 0;
return new_char_array;
} // end of strcat_new
strcat_new.h
#ifndef _STRCAT_NEW_H_
#define _STRCAT_NEW_H_
/*
* strcat_new:
*
* 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:
* strcat_new 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 *strcat_new(char *delim, long num_args, ...);
#endif
test_strcat_new.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "strcat_new.h"
int main(void)
{
char *a = strcat_new(";:?", 4, "abc", "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = strcat_new(NULL, 3, "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = strcat_new(NULL, 0, "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, -1, "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new("=", 4, "abc", "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = strcat_new("{(=%$^%^&&(&)}", 4, "abc", "123", "xyz", "455");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
printf("\n");
}
strjoin
rather thanstrcat_new
, which makes one think it's similar tostrcat
, when it's really very different. \$\endgroup\$strjoin
, and a varargs-based interface would be calledvstrjoin
(by analogy withsprintf
/vsprintf
etc.). Evidently, one should just call the other after conversion of the arguments. We'll leave it as an exercise to determine which one calls which :-) \$\endgroup\$