×

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string.

Note:   To use this function we must include the #include<string.h> header file in our program. This<string.h> header file contains all the required functions that are needed to perform operations on a string.

The string functions concerning operations they performed are given below :

  • strlen()          gives the length of a string.
  • strcmp()compares two strings.
  • strcat()          concatenates two strings
  • ()                   copies one string into another.
  • strchr()         find the first occurrence of a given character in a string.
  • strstr()           findthe occurrence of thefirst string in a string.
  • strcasecmp()            without sensitivity to the case compares two strings.

These functionsare defined/inbuilt to reduce the complexity of performing the operations on a string.

1.strcpy()
strcpyis the abbreviationfor the string copy. This function is mainly used to copy one string into another string and provide a null character to terminate the string. The strings are the source string and destination string.
i.e, the content of the source string copied into the destination string.

Syntax:

strcpy(destination string , source string);

Or

char* strcpy(char* destination, const char* source);
  • From syntax, we can say that the source string is after the destination string.
  • When u see the second syntax the question may arise why const keyword at source string? This is because in the whole process we are not modifying the source we are simply coping the content in the source string to the destination string.

Important Points:

  • Compare to the length of the source string the length of the destinations string should be greater than or equal.
  • In this function when coping source string to destination string then the source string includes the null character.

Example 1:

For example, consider the length of the destination string is 5 and the source string is 10. Then, from the source string, only 5 characters will be copied into the destination string, and the remaining 10 characters won’t be copied and will be truncated.

char a[10]=”javatpoint”;
char b[5];
strcpy(b,a);

Output:

error

Example 2:

For example, consider the length of the destination string is10 and the source string is 10. Then, 10 characters are copied tothe destination string.

char a[10]=”javatpoint”;
char b[10];
strcpy(b,a);

Output:

javatpoint

Program

#include<stdio.h>
#include<string.h>
int main() {
char a[10]=”javatpoint”;
char b[10];
printf(“%s\n”,strcpy(b,a));//strcpy function
return 0;
}

Output:

javatpoint

Note:      strcpy returns the pointer to the first character of the string which is copied to the destination. Then the whole string will be printed on the screenby using the %s.

We can also chain together a series of strcpy calls i.e,the strcpy function is stored or performed by another strcpy function. Like nested strcpy function. The example is shown below.

Program

#include<stdio.h>
#include<string.h>
int main() {
char a[10]=”javatpoint”;
char b[10];
char c[10];
strcpy(c,strcpy(b,a))//strcpy within strcpy
printf(“%s\n%s\n”,b,c);
return 0;
}

Output:

Javatpoint
javatpoint

If the length of the string pointed by the source string is greater than the length of the character array destination string then it will be undefined behaviour as shown in the below program.

Program

#include<stdio.h>
#include<string.h>
int main() {
char a[10]=”javatpoint”;
char b[5];
printf(“%s\n”,strcpy(b,a));//strcpy function
return 0;
}

Output:

Undefined behavior

To avoid this we can call the strncpy function instead of strcpy.strncpy is the abbreviation of string n copy. n number of characters are copied by using this function. The drawback of the strcpy() is overcome by the strncpy().

Syntax:

strncpy(destination string , source string,siZeof(destination));

Program

#include<stdio.h>
#include<string.h>
int main() {
char a[10]=”javatpoint”;
char b[5];
strncpy(b,a,sizeof(b))//strncpy function
return 0;
printf(“%s\n”,b);
}

Output:

javat

strncpy will leave the string in its destination without a terminating null character if the size of the source is equal to or greater than the size ofthe destination. For this purpose, we add an extra line in our program as shown in below.

Program

#include<stdio.h>
#include<string.h>
int main() {
char a[11]=”javatpoint”;
char b[11];
strncpy(b,a,sizeof(b))//strncpy function
b[sizeof(b)-1]=’\0’;//add null character
return 0;
printf(“%s\n”,b);
}

Output:

Javatpoint

In the above program, we have added the null character in the destination.


Related Topics

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.