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 the standard C library
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 the standard C library
Became Hot Network Question
#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(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;delim_len;
}
va_end(valist);
if (total_len == 0)
= total_len - delim_len; // remove the returnlast NULL;
delimiter
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_concati < (num_args - 1)) {
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 strcat_new
#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(const char *delim, long num_args, ...);
#endif
#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);
a = strcat_new(";;;", 4, "abc", NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = strcat_new(";;;", 4, "abc", NULL, NULL, "cde");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = strcat_new(";;;", 4, NULL, NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 4, NULL, NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 4, NULL, "fdr", NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 1, NULL, "fdr", NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
printf("\n");
}
#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(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) {
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 strcat_new
#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(const char *delim, long num_args, ...);
#endif
#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);
a = strcat_new(";;;", 4, "abc", NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = strcat_new(";;;", 4, "abc", NULL, NULL, "cde");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = strcat_new(";;;", 4, NULL, NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 4, NULL, NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 4, NULL, "fdr", NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 1, NULL, "fdr", NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
printf("\n");
}
#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
#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
#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");
}
#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(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 + delim_len;1;
}
va_end(valist);
if (total_len === total_len0)
- delim_len; // remove the last delimiter 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 (i < (num_args - 1)num_delim_to_concat) {
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 strcat_new
#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(const char *delim, long num_args, ...);
#endif
#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);
a = strcat_new(";;;", 4, "abc", NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = strcat_new(";;;", 4, "abc", NULL, NULL, "cde");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = strcat_new(";;;", 4, NULL, NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 4, NULL, NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 4, NULL, "fdr", NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 1, NULL, "fdr", NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
printf("\n");
}
#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
#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
#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");
}
#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(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) {
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 strcat_new
#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(const char *delim, long num_args, ...);
#endif
#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);
a = strcat_new(";;;", 4, "abc", NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = strcat_new(";;;", 4, "abc", NULL, NULL, "cde");
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, strlen(a));
free(a);
a = strcat_new(";;;", 4, NULL, NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 4, NULL, NULL, NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 4, NULL, "fdr", NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
a = strcat_new(NULL, 1, NULL, "fdr", NULL, NULL);
printf("\n");
printf("a = %s, strlen(a) = %lu\n", a, a?strlen(a):0);
free(a);
printf("\n");
}
lang-c