Join array of strings into a single string : String Convert « String « C / ANSI-C

C / ANSI-C
1. assert.h
2. Console
3. ctype.h
4. Data Structure Algorithm
5. Data Type
6. Development
7. File
8. Function
9. Language Basics
10. Macro Preprocessor
11. Math
12. math.h
13. Memory
14. Pointer
15. setjmp.h
16. signal.h
17. Small Application
18. stdio.h
19. stdlib.h
20. String
21. string.h
22. Structure
23. time.h
24. wctype.h
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C / ANSI-C » String » String ConvertScreenshots 
Join array of strings into a single string

/*
Beginning C, Third Edition
 By Ivor Horton
 ISBN: 1-59059-253-0
 Published: Apr 2004
 Publisher: apress

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_STRINGS 100                         /* Maximum string count                        */
#define BUFFER_SIZE 50                          /* Initial input buffer size                   */

char* join_strings(char *strings[]int count)/* Joins array of strings into a single string */
char* read_string();                            /* Reads a string from the keyboard            */

void main()
{
  char *pStrings[MAX_STRINGS];       /* Array of pointers to strings */
  char *joined_strings = NULL;       /* Pointer to the joined string */
  int count = 0;                     /* Number of strings entered    */
  char answer = 'y';                 /* Confirms more input          */
  char terminator = '*';             /* Terminator for string entry  */
  int i = 0;

  /* Read the strings */
  while(count<MAX_STRINGS && tolower(answer)=='y')
  {
    printf("Enter a string:\n");
    pStrings[count++= read_string(terminator);

    printf("Do you want to enter another: ");
    scanf(" %c", &answer);
    fflush(stdin);                     /* Lose newline following character entry */
  }

  joined_strings = join_strings(pStrings, count)
  printf("\nHere are the strings as a single string:\n%s\n", joined_strings);

  free(joined_strings);                /* Free memory for joined strings   */
  for(i = ; i<count ; i++)           /* Free memory for original strings */
    free(pStrings[i]);
}

/************
 * Function to join an array of strings                            *
 * this function allocates memory that must be freed by the caller *
 ************/
char* join_strings(char *strings[]int count)
{
  char* str = NULL;             /* Pointer to the joined strings  */
  size_t total_length = 0;      /* Total length of joined strings */
  size_t length = 0;            /* Length of a string             */
  int i = 0;                    /* Loop counter                   */

  /* Find total length of joined strings */
  for(i = ; i<count ; i++)
  {
    total_length += strlen(strings[i]);
    if(strings[i][strlen(strings[i])-1!= '\n')
      ++total_length; /* For newline to be added */
  }
  ++total_length;     /* For joined string terminator */

  str = (char*)malloc(total_length);  /* Allocate memory for joined strings */
  str[0'\0';                      /* Empty string we can append to      */

  /* Append all the strings */
  for(i = ; i<count ; i++)
  {
    strcat(str, strings[i]);
    length = strlen(str);

    /* Check if we need to insert newline */
    if(str[length-1!= '\n')
    {
      str[length'\n';             /* Append a newline       */
      str[length+1'\0';           /* followed by terminator */
    }
  }
  return str;
}

/********************
 * Reads a string of any length.                                           *
 * The string is terminated by the chracter passed as the argument.        *
 * Memory is allocated to hold the string and must be freed by the caller. *
 ********************/
char* read_string(char terminator)
{
  char *buffer = NULL;            /* Pointer to the input buffer */
  int buffersize = BUFFER_SIZE;   /* Current buffer capacity     */
  int length = 0;                 /* String length               */
  char *temp = NULL;              /* Temporary buffer pointer    */
  int i = 0;                      /* Loop counter                */

  buffer = (char*)malloc(BUFFER_SIZE);  /* Initial buffer */
  /* Read the string character by character */
  for(;;)
  {
    /* Check for string terminator */ 
    if((buffer[length= getchar()) == terminator)
      break;
    else
      ++length;

    /* Check for buffer overflow */
    if(length == buffersize)
    {
      buffersize += BUFFER_SIZE;          /* Increase buffer size */
      temp = (char*)malloc(buffersize);   /* Allocate new buffer  */

      /* Copy characters from old buffer to new */
      for(i = ; i<length ; i++)
        temp[i= buffer[i];

      free(buffer);                       /* Free memory for old buffer */
      buffer = temp;                      /* Store new buffer address   */
      temp = NULL;                        /* Rest temp pointer          */
    }
  }
  buffer[length'\0';                  /* Append string terminator                  */
  temp = (char*)malloc(length+1);         /* Allocate exact memory required for string */
  strcpy(temp, buffer);                   /* Copy the string       */
  free(buffer);                           /* Free the buffer memory */
  return temp;
}


           
       
Related examples in the same category
1. Convert one or more floating-point values into a single string
2.  Convert string to double-precision floating-point value
3. Change string to integer
4. Convert string entered to integer
5. Convert string entered to long
6.  Convert string to double: How to use atof: sines calculator
7.  Convert string to integer: atoi
8.  Convert string to long : atol
9.  Print formatted data to a string: how to use sprintf
10. Convert string to double: how to use atof
11.  Convert string to double-precision floating-point value: how to use strtod
12.  Convert string to long integer: how to use strtol
13.  Convert string to unsigned long integer: how to use strtoul
14. Convert string to int, double and long
15. Absolute value of long integer: how to use labs
w__ww___.___j_av__a__2_s___.c__o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.