strcat in c

If you want to know about strcat in c then read this blog post.

What is strcat in c programming?

strcat in c appends source string to the destination string. It is defined in string.h header file.

strcat function returns pointer to the destination string.

Prototype

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

Parameters for strcat in c

strcat takes two parameters. First string and string which is to be appended.

Return value of strcat in c

It returns pointer to the destination string.

strcat example program in c

Example 1

//strcat example 1
#include<stdio.h>
#include<string.h>
int main()
{
   char src[40]="soul";
   char dest[40]="scholar";
   strcat(dest,src);
   printf("After concatenating :%s \n",dest);
   return 0;
}
strcat example in c 1
strcat example in c 1
	After concatenating :scholarsoul 

Code explanation

The source string is appended to the destination string by calling strcat function. The result is being printed onto the console.


Exmaple 2

//strcat example 2
#include<stdio.h>
#include<string.h>
int main()
{
   char src[40];
   char dest[40];
   printf("\n\n\tEnter destination string.\n\t");
   scanf("%s",dest);
   printf("\n\tEnter source string.\n\t");
   scanf("%s",src);
   strcat(dest,src);
   printf("\n\tAfter concatenating both strings:\n\t%s \n\n",dest);
   return 0;
}
strcat example in c 2
strcat example in c 2
	Enter destination string.
	scholar 

	Enter source string.
	soul

	After concatenating both strings:
	scholarsoul 

Code explanation

The source string is appended to the destination string by calling strcat function. The result is being printed onto the console.


Difference between strcat and strncat in c

strcat in c appends the source string to the destination string. strncat appends source string to the destination string up to n characters.

strcat returns pointer to the string destination. strncat also returns pointer to the destination string.

strcat takes two arguments. strncat takes three arguments.

strcat appends entire source string to the destination. strncat appends only specified characters to the destination.

General forms are as follows

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

char *strncat(char *dest,const char*source,size_t m);
strcat in cstrncat in c
Appends source string to the destination string.Appends source string to the destination string upto n characters.
char *strcat(char *dest,const char*source);
char *strncat(char *dest,const char*source,size_t m);
Takes two arguments.Takes three arguments.
Appends entire source string to the destination.Appends only specified characters to the destination.

Difference between strcat and strlen in c

strlen in c is used to calculate the length of a string. strcat in c appends source string to the destination string.

strlen returns total length of a string. strcat returns pointer to the string destination.

strlen terminates when it encounters null character. strcat terminates when the process is over.

strlen returns integer value. strcat returns pointer to the destination string.

General forms are as follows

int  strlen(const char*strng);

char *strcat(char *dest,const char*source);
strcat in cstrlen in c
Appends source string to the destination string.Used to calculate the length of a string.
char *strcat(char *dest,const char*source);int strlen(const char*strng);
Returns pointer to the string destination.Returns total length of a string.
Terminates when the process is over.Terminates when it encounters null character.

Difference between strcat and strrev in c

strrev in c is used to reverse a given string. strcat in c appends source string to the destination string.

strrev returns a reversed string. strcat returns pointer to the string destination.

strrev takes one argument. strcat takes two arguments.

General forms are as follows


char *strrev(char * strin);

char *strcat(char *dest,const char*source);
strrev in cstrcat in c
Used to reverse a given string.Appends source string to the destination string.
char *strrev(char * strin);char *strcat(char *dest,const char*source);
Takes one argument.Takes two arguments.

Difference between strcat and strcpy in c

strcpy is used to copy the source string to the destination string. strcat in c appends the source string to the destination string.

strcpy returns pointer to the destination string. strcat returns pointer to the string destination.

General forms are as follows


char *strcpy(char *destn,const char*sorc);

char *strcat(char *dest,const char*source);
strcat in cstrcpy in c
Appends source string to the destination string.Used to copy the source string to destination string.
char *strcat(char *dest,const char*source);char *strcpy(char *destn,const char*sorc);
Returns pointer to the string destination.Returns pointer to the destination string.

Difference between strcat and strcmp in c

strcmp function is used to compare two strings character by character. strcat in c appends the source string to the destination string.

strcmp function returns 1.returns value<0 if st1 is less than st2. 2.it returns value>0 if st2 is less than st1. 3. it returns value=0 if both are equal. strcat returns pointer to the string destination.

strcmp returns one of the three integer value(negative,positive,zero). strcat returns pointer to the string destination.

General forms are as follows


int strcmp(const char*st1,const char*st2);


char *strcat(char *dest,const char*source);
strcmp in cstrcat in c
Used to compare two strings character by character.Appends source string to the destination string.
int strcmp(const char*st1,const char*st2);char *strcat(char *dest,const char*source);
Returns 1.returns value<0 if st1 is less than st2. 2.it returns value>0 if st2 is less than st1. 3. it returns value=0 if both are equal.Returns pointer to the string destination.

People also ask for

What is the difference between strchr and strcat in c?

strchr is used to search for the first occurrence of a character in the string pointed to by string argument. strcat is used for concatenation purposes.

What is the use of strcat () function?

strcat function concatenates source string and destination string then stores result in destination string’s space.

Is Strcat safe?

strcat is not safe as it can be misused by attcker by buffer overflow attack.


People also read