The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

strcat() in C

Purpose of strcat()

strcat() is one of the inbuilt string function in c programming which is used to combine two strings to form a single one. The full form of strcat() is string concatenation.

How strcat() Works

The following diagram clearly illustrate the working principle of strcat() inbuilt string function in C.

strcat() in c

In the above diagram strcat() takes two parameters say str1 and str2. Here str2 will be completely appending to str1.

Syntax - strcat()

  • strcat() accepts two parameters.
  • Both parameters must be a string.
  • To use strcat() inbuilt string function in C, we need to declare #include<string.h> header file.
Syntax
strcat (str1, str2);

C Program - Using strcat()

c-strcat-1.c
#include <stdio.h>
#include<string.h>
int main()
{
char str1[30] = "How are ", str2[30] = "you";
strcat (str1, str2);
printf("Str1 : %s ", str1);
return 0;
}
Str1 : How are you

Note:

In the above program, strcat() simply concatenate two strings i.e str2 with str1.

C Program - Without strcat()

c-strcat-2.c
#include <stdio.h>
#include<string.h>
int main()
{
char str1[30] = "How are ";
char str2[30] = "you";
int i, j;
for(i=0; str1[i]!='\0';i++){
//making using of variable i
}
for (j=0;str2[j] !='\0';j++){
str1[i] = str2[j];
i++;
}
printf("%s", str1);
return 0;
}
Str1 : How are you

Note:

The above program looks verbose but yields same the result.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report