×

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

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.