khuang8493
7/4/2018 - 11:03 PM

Allocate space, copy and combine 2 strings

First allocate enough space for a string, then copy a string into the allocated space.

After that, reallocate the same string make more space, and then add another string to the previous one.

1. C library function - malloc()

Description
The C library function void *malloc(size_t size) allocates the requested memory and returns a pointer to it.

Declaration
Following is the declaration for malloc() function.

void *malloc(size_t size)
Parameters
size − This is the size of the memory block, in bytes.

Return Value
This function returns a pointer to the allocated memory, or NULL if the request fails.

Example:

    // get a string
    char *s = get_string("s: ");
    if (!s)
    {
        return 1;
    }
 
    // allocate memory for another string
    char *t = malloc((strlen(s) + 1) * sizeof(char));
    
    if (!t)
    {
        return 1;
    }
 
    // copy string into memory
    for (int i = 0, n = strlen(s); i <= n; i++)
    {
        t[i] = s[i];
    }
 
* Copy string contents into the new memory block can also be written using "strcpy(des, src);" The result is the same. Main thing is to allocate new memory block for string "t" first.

2. strcpy - copy a string

#include <string.h>
string strcpy(string destination, string source);

DESCRIPTION
strcpy copys string source into string destination.

RETURN VALUE
A string, destination, is returned.

EXAMPLES
string source = "CS50";
char destination[50];

strcpy(destination, source);

printf("%s is the same as %s: %d\n", destination, source, strcmp(source, destination));

Output:

CS50 is the same as CS50: 0


3. strcat, strncat - concatenate two strings

#include <string.h>
char * strcat(char * dest, const char * src);

char * strncat(char * dest, const char * src, size_t n);
The strcat() function appends the src string to the dest string, overwriting the terminating null byte (\0) at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.

The strncat() function is similar, except that

it will use at most n bytes from src; and

src does not need to be null-terminated if it contains n or more bytes.

As with strcat(), the resulting string in dest is always null-terminated.

If src contains n or more bytes, strncat() writes n+1 bytes to dest (n from src plus the terminating null byte). Therefore, the size of dest must be at least strlen(dest)+n+1.

Example using allocation and adding 2 strings:

#include <stdio.h>
#include <stdlib.h>

int main () {
   char *str;

   /* Initial memory allocation */
   str = (char *) malloc(15);
   strcpy(str, "tutorialspoint");
   printf("String = %s,  Address = %u\n", str, str);

   /* Reallocating memory */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);

   free(str);
   
   return(0);
}

Let us compile and run the above program that will produce the following result −

String = tutorialspoint, Address = 355090448
String = tutorialspoint.com, Address = 355090448