How to Implement Function Strncpy & Strcpy in C?

C has libraries that are rich in functions. One of those functions is strcpy(). Strcpy in C is used to copy the strings that are pointed by the source to the destination. The pointed strings are copied and returned by the function.

The strcpy syntax is:  char* strcpy (char*destination, const char* source)

Write a program to copy the contents of one string to another string Using strcpy in C.

Flowchart for program 

Flowchart Strcpy in c

Algorithm for program 

  1. Start
  2. Include the header file string.h.
  3. Declare two characters.
  4. Get characters from the user.
  5. Use the strcpy() function to copy one string to another in C.
  6. Print both the strings to the string.
  7. End

Also Read: What is the use of Associativity and Operator Precedence in C?

Program 1

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

int main()
{
char carr1[50]; //Declare source string
char carr2[50]; //Declare destination string
printf(“Input string 1: \n”); //Enter source string
gets(carr1);
printf(“Input strng 2: \n”); //Enter destination string
gets(carr2);
strcpy(carr2, carr1); //Function Strcpy in C copy the content of carr1 into carr2
printf(“First string = %s \n”, carr1);
printf(“Second string = %s \n”, carr2);
return 0;
}

Output

strcpy in c

Explanation

The above program explains the process of string copying. Strcpy in C  is used to simplify this process. The user inputs two strings of a specific size using the gets function. Then strcpy function is used to copy the content of string 1 into string 2. Then both the strings are printed.

Let us look at one more program that uses the strcpy function in C to copy the content of a string.

Algorithm for Program 2

  1. Start
  2. Include header file “string.h”.
  3. Declare two characters namely string1 and string2.
  4. The first character contains string – “Hey There!”
  5. Use the strcpy function. It will contain two arguments – string2 and string1. Puts function will be used to print the copied answer.
  6. Print both the strings.
  7. End.

Also Read: What is Storage Class in C Language?

Program 2

#include <stdio.h>

#include <string.h>

int main()

{

char string1[20]= “Trickyedu”;

char string2[20];

strcpy(string2, string1); // Function strcpy in C copy the content of string 1 into string 2

puts(string2);

printf(“First string =%s \n”, string1);

printf(“Second string =%s \n”, string2);

return 0;

}

Output

strcpy in c

Implement Strcpy() Function Using Pointer

The function strcpy in C can be implemented by using pointers. Here, two arguments of type pointer variables will be used. The function will then return a pointer to the first string.

Also Read: What are C Tokens? 

Flowchart for Program 

implementation of strcpy in c using pointer

Algorithm for Program 

  1. Start the program
  2. Include header file
  3. Declare function
  4. Initialize string 1 and string 2
  5. Initialize pointer p and q
  6. Assign base address of string1 and string2 to pointer p and q
  7. Calling function
  8. Run the while loop until all the contents of string 1 are copied into string 2.
  9. Print the final source and destination string
  10. End the program

Also Read: Operators in C

Program 3

#include <stdio.h>
char copy(char*, char*); //Declare Copy Function
int main()
{
char s1[]=”Trickyedu.com”; //Initialize source string
char s2[20]; //Declare Destination String
char *p,*q; //Declare Pointer p and q
p=s1; //Initialize pointer p with the initial address of string s1
q=s2; //Initialize pointer q with the initial address of string s2
copy(p,q); //Calling Copy function
printf(“%s\n%s\n”,p,q);
return 0;
}
//Definition of copy function
char copy(char *p, char *q)
{
while(*p!=’\0′)
{
*q=*p;
q++;
p++;
}
*q=’\0′;
return *q;
}

Explanation

This program uses pointers. We first create the function copy that will accept arguments of type pointer. These pointers will point to the source and the destination.

Inside the function, loop while will run. The while loop will copy the characters of string 1 into string 2 one by one until the pointer points to the null address ‘\0’. The function will return started.

In the main function, two characters are declared. These characters are input by the users. Then copy function is used to copy the content of string 1 into string 2. Both the strings are printed.

What is Strncpy in C? 

One of the many useful functions contained by C’s libraries is strncpy. This function means – bounded string copy. The strncpy() function is used to copy the first n characters of one string to the other string. In case the function encounters null characters, it will copy null characters until n characters have been generated.

The syntax of strncpy() function is –

char *strncpy(char *string1, const char *string 2, size_o n);

Let us now look at a sample program to understand the working of strncpy function.

Write a program to copy the number of characters string to another string Using strncpy()

Algorithm 

  1. Start.
  2. Include the header file string.h
  3. Declare two characters.
  4. Create a string “Hello User! This is trickyedu.com” and copy it completely to s2 using function strcpy in C.
  5. Copy the first 10 characters of s2 into s1 using  strncpy() function.
  6. Print both the strings.
  7. End.

Program 

#include <stdio.h>

#include <string.h>

int main () {

char s1[40]; //Declare string1

char s2[45]; //Declare string2

strcpy(s2, “Hello User! This is trickyedu.com”); //strcpy in C copy the string into destination string s2

strncpy(s1, s2, 10);

printf(“This is string s1: %s\n”, s1);

printf(“This is string s2: %s\n”, s2);

return 0;

}

Explanation 

In the above piece of code, we are trying to copy the first n characters for string 2 into string 1. We started the program by including the necessary stdio.h and string.h files. Then we started out main program.

We first declare two characters of lengths 40 and 45 each.

We then used the function strcpy in c  to completely copy the content of s2 into s1. After that, we the strncpy() function to perform a limited operation where only 10 characters of s2 were copied into s1. We then used the printf() function to print both the strings to the screen.

Implement Function Strncpy in C Using Pointers

using pointers. Here, three arguments will be used. Two of them will be of type pointers and will point to the source and the destination. The third argument will tell the compiler how many numbers of characters you want to copy into another string. Let us take a look at a sample program to further understand this.

Algorithm 

  1. Start.
  2. Create function char* strncpy() before starting the main program. In this function, use three arguments – char* destn, const char* src, and size_t n.
  3. Inside the function, if destn is NULL, return NULL and put the value of destn into char* ptr.
  4. Run a while loop where *src && n–. Inside the loop, put the value of *src into *destn. Increment destn and src by using ++ operator. Close the while loop and return ptr. Close the function.
  5. Start the main function. Set the value of char* src as “Hello there!!!”.
  6. Set the size of char destn as 20.
  7. The size of size_t n will be 7.
  8. Copy the string using strncpy and print it to the screen.
  9. End.

Program 

#include <stdio.h>
#include <stddef.h>
//Declaration strncpy function
char* strncpy(char* destn, const char* src, size_t n)
{
if (destn == NULL)
return NULL;
char* ptr = destn;
while (*src && n–)
{
*destn = *src;
destn++;
src++;
}
return ptr;
}
int main(void)
{
char* src = “Hello there!!!”; //Initialize source string
char destn[20]; //Initialize Destination string
size_t n = 7; //Initialize number of elements you want to copy in another string
printf(“%s”, strncpy(destn, src, n)); //calling strncpy(destn,src,n) function
return 0;
}

Explanation 

We start our program by including the usual header files.

We then create a function called char* strncpy with arguments – char* destn, const char* src, size_t n.

Inside the function, we set some conditions. If the destn is NULL i.e empty, the function will return NULL. The value of destn is then put into a variable char* ptr. Then, we run a while loop where the value of src is put into destn.

The ++ operator is then used for both of them. The while loop is closed and the function ends after returning ptr. In the main function, char* src is assigned the value = “Hello There!!!”. The destn is assigned a size of 20 while the size_t n gets is assigned the size 7.

Now the strncpy function copy 7 characters of src string to destn string. This is printed on the screen.

Hello, My Name is Abhinav. I am an Author in the Education Category of Trickyedu. I have Done My Engineering in Computer Science from DIT University. I have a good command on Science, Programming Language, and microprocessors. So, I choose this platform to share my knowledge and experience.

Leave a Comment