C Program to Copy a String Using strcpy() (strcpy() फंक्शन द्वारा एक स्ट्रिंग को दूसरी स्ट्रिंग में कॉपी करने का C प्रोग्राम)

What is strcpy() Function in C?

In C language, the strcpy() function is used to copy one string into another string. It copies all characters of the source string, including the null character ('\0'), into the destination string.

The strcpy() function is declared in the standard header file string.h.

C भाषा में strcpy() फंक्शन क्या है?

C लैंग्वेज में strcpy() फंक्शन का प्रयोग एक String को दूसरी String में Copy करने के लिए किया जाता है। यह Source String के सभी Characters को Null Character ('\0') सहित Destination String में Copy करता है।

strcpy() फंक्शन को C भाषा की Standard Header File string.h में Declare किया गया है।

Syntax of strcpy() Function

strcpy(destination, source);

Function Prototype:

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

Here:

  • strcpy() is the name of the function.

  • destination is the character array in which the string will be copied.

  • source is the original string that needs to be copied.

व्याख्या:

  • strcpy() फंक्शन का नाम है।

  • destination वह Character Array है जिसमें String को Copy किया जाता है।

  • source वह Original String है जिसे Copy करना होता है।

Note: The destination array must have sufficient memory to store the complete source string, including its null character.

नोट: Destination Array में Source String के सभी Characters तथा Null Character को Store करने के लिए पर्याप्त Memory होनी चाहिए।

Example of strcpy() Function

char str1[20];
char str2[] = "India";
strcpy(str1, str2);

Output:

str1 = India

Explanation:

In the above example, the string "India" stored in str2 is copied into str1 using the strcpy() function.

व्याख्या:

उपरोक्त उदाहरण में str2 में Store String "India" को strcpy() फंक्शन की सहायता से str1 में Copy किया गया है।

C Program to Copy a String Using strcpy()

Program:

#include <stdio.h>
#include <string.h>
int main()
{
char str1[255];
char str2[255];
printf("Enter a string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0';
strcpy(str2, str1);
printf("Original String = %s\n", str1);
printf("Copied String = %s\n", str2);
return 0;
}

Output:

Enter a string: Computer Science
Original String = Computer Science
Copied String = Computer Science

Explanation:

  1. stdio.h is used for input and output operations.

  2. string.h is included to use the strcpy() and strcspn() functions.

  3. Two character arrays str1[255] and str2[255] are declared.

  4. fgets() is used to accept a string from the keyboard.

  5. strcspn() removes the newline character from the input string.

  6. strcpy(str2, str1) copies the contents of str1 into str2.

  7. Finally, printf() displays both the original and copied strings.

व्याख्या:

  1. stdio.h हेडर फाइल का उपयोग Input एवं Output Operations के लिए किया जाता है।

  2. string.h हेडर फाइल को strcpy() एवं strcspn() फंक्शन्स के उपयोग के लिए शामिल किया गया है।

  3. दो Character Arrays str1[255] एवं str2[255] Declare किए गए हैं।

  4. fgets() Function का उपयोग Keyboard से String Input करने के लिए किया गया है।

  5. strcspn() Function Input String से Newline Character को हटाता है।

  6. strcpy(str2, str1) Statement की सहायता से str1 की String को str2 में Copy किया जाता है।

  7. अंत में printf() Function Original एवं Copied Strings को Display करता है।

Important Points to Remember

  1. strcpy() is a standard string handling function in C.

  2. It is declared in the string.h header file.

  3. It copies the source string into the destination string.

  4. It also copies the null character ('\0').

  5. The destination array must have sufficient memory.

  6. The original source string remains unchanged.

  7. The source and destination strings must not overlap.

महत्वपूर्ण बिंदु:

  1. strcpy() C भाषा का एक Standard String Handling Function है।

  2. यह string.h Header File में Declare किया गया है।

  3. यह Source String को Destination String में Copy करता है।

  4. यह Null Character ('\0') को भी Copy करता है।

  5. Destination Array में पर्याप्त Memory होनी चाहिए।

  6. Original Source String में कोई परिवर्तन नहीं होता है।

  7. Source एवं Destination Strings के Memory Locations एक-दूसरे से Overlap नहीं होने चाहिए।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025