Site icon DataFlair

Difference Between strcpy() and strcat() Function in C

strcpy() vs strcat() in c

String manipulation is an essential concept in C programming. The C standard library provides functions like strcpy() and strcat() to copy and concatenate strings efficiently. In this post, we’ll examine how strcpy() and strcat() are used as well as some of their main distinctions.

The strcpy() Function in C

The strcpy() function inserts the whole string (including the null terminator) that is referenced by the source into the destination array.

The syntax is:

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

Let’s look at an example:

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

int main() {

  char source[20] = "Hello World";
  char destination[20];

  strcpy(destination, source);

  printf("Source string: %s\n", source);
  printf("Destination string: %s\n", destination);
  
  return 0;
}

Output:
Source string: Hello World
Destination string: Hello World

Here, the source string “Hello World” is copied into the destination array using strcpy().

Some key points about strcpy():

The strcat() Function in C

The strcat() function concatenates two strings by appending a copy of source to the end of destination.

The syntax is:

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

Let’s look at an example:

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

int main() {

  char str1[20] = "Hello";
  char str2[20] = "World";

  strcat(str1, str2);

  printf("str1 before concat: %s\n", str1);
  printf("str2 string: %s\n", str2);
  printf("Concatenated string: %s\n", str1);

  return 0;
}

Output:
str1 before concat: Hello
str2 string: World
Concatenated string: HelloWorld

Here, strcat() appends str2 to str1, resulting in the concatenated string.

Key properties of strcat():

Common Example

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

int main() {
    char name1[10] = "DataFlair";
    char name2[10] = "Rohan"; // Changed from "John" to "Rohan"
    char name3[10];

    strcpy(name3, name1);
    printf("%s\n", name3); // Output: DataFlair

    strcat(name1, name2);
    printf("%s\n", name1); // Output: DataFlairRohan

    return 0;
}

Explanation:

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

int main() {

  char source[100] = "This is a source string";
  char dest[100];
  
  // Copy source to dest using strcpy()
  strcpy(dest, source);
  
  // Concatenate additional string using strcat()
  strcat(dest, " - Extra appended text"); 
  
  printf("Source string: %s\n", source);
  printf("Destination string: %s\n", dest);
  
  return 0;
}

Output:

Source string: This is a source string
Destination string: This is a source string – Extra appended text

In this program:

When to Use Each Function

Based on their behavior, here are some guidelines on when to use each one:

Key Differences Between C strcpy() and strcat()

Feature strcpy() strcat()
Behavior Copies source string to destination Appends source string to destination
Overwriting Overwrites destination fully Does not overwrite, only appends
Null-termination Not required in the destination Required in destination before concatenating
Return value Returns destination string Returns concatenated string
Performance Faster Slower
Use cases Complete replacement of destination needed Appending strings by concatenating
Buffer issues Can cause overflow if destination is too small Can cause overflow if destination is too small after appending

Conclusion

In summary, strcpy() and strcat() allow efficient string manipulation in C but have some key differences. strcpy() overwrites the destination while strcat() appends to it. Keeping their exact behavior in mind helps us pick the right function and use it safely by managing null termination and buffers.

Exit mobile version