Question #5749   Submitted by Answiki on 01/22/2022 at 06:25:34 PM UTC

How to concatenate two strings in C?

Answer   Submitted by Answiki on 01/22/2022 at 06:29:42 PM UTC

In C, the easiest way to concatenate two strings is to use the strcat() function whose prototype is shown below:

char * strcat( char * destination, const char * source );

Parameters:

  • dest is a pointer to the destination string which must also contain the first string. This string must be large enough to contain the result of the concatenation.
  • src is the string to add to the dest string.


Returned value: the function returns a pointer to the result string (dest)


The function adds the string source to the end of the string destination. In a symbolic way : destination = destination + source . Here is an example:

#include <stdio.h>
#include <string.h>

int main()
{
	char src[] = "begin";
	char dest[50] = "end";

	strcat(dest, src);
    // Affiche : "beginend"
	printf(dest);

	return 0;
}


The strcat() function belongs to the string.h library. The function returns the address of the string resulting from the concatenation (a pointer to the string destination). This can be used to cascade multiple calls to the strcat() function: 

// Concaténe str1 + str2 + str3 => str1
strcat (str1, strcat(str2, str3) );



3 events in history
Answer by Answiki on 01/22/2022 at 06:29:42 PM

In C, the easiest way to concatenate two strings is to use the strcat() function whose prototype is shown below:

char * strcat( char * destination, const char * source );

Parameters:

  • dest is a pointer to the destination string which must also contain the first string. This string must be large enough to contain the result of the concatenation.
  • src is the string to add to the dest string.


Returned value: the function returns a pointer to the result string (dest)


The function adds the string source to the end of the string destination. In a symbolic way : destination = destination + source . Here is an example:

#include <stdio.h>
#include <string.h>

int main()
{
	char src[] = "begin";
	char dest[50] = "end";

	strcat(dest, src);
    // Affiche : "beginend"
	printf(dest);

	return 0;
}


The strcat() function belongs to the string.h library. The function returns the address of the string resulting from the concatenation (a pointer to the string destination). This can be used to cascade multiple calls to the strcat() function: 

// Concaténe str1 + str2 + str3 => str1
strcat (str1, strcat(str2, str3) );



Answer by Answiki on 01/22/2022 at 06:29:19 PM

In C, the easiest way to concatenate two strings is to use the strcat() function whose prototype is shown below:

char * strcat( char * destination, const char * source );

Parameters:

  • dest is a pointer to the destination string which must also contain the first string. This string must be large enough to contain the result of the concatenation.
  • src is the string to add to the dest string.


Returned value: the function returns a pointer to the result string (dest)


The function adds the string source to the end of the string destination. In a symbolic way : destination = destination + source . Here is an example:


#include <stdio.h>
#include <string.h>

int main()
{
	char src[] = "begin";
	char dest[50] = "end";

	strcat(dest, src);
    // Affiche : "beginend"
	printf(dest);

	return 0;
}


The strcat() function belongs to the string.h library. The function returns the address of the string resulting from the concatenation (a pointer to the string destination). This can be used to cascade multiple calls to the strcat() function: 

// Concaténe str1 + str2 + str3 => str1
strcat (str1, strcat(str2, str3) );



Question by Answiki 01/22/2022 at 06:25:34 PM
How to concatenate two strings in C?
# ID Query URL Count

Icons proudly provided by Friconix.