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.destinationis the character array in which the string will be copied.sourceis 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 = IndiaExplanation:
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 ScienceOriginal String = Computer ScienceCopied String = Computer Science
Explanation:
stdio.his used for input and output operations.string.his included to use thestrcpy()andstrcspn()functions.Two character arrays
str1[255]andstr2[255]are declared.fgets()is used to accept a string from the keyboard.strcspn()removes the newline character from the input string.strcpy(str2, str1)copies the contents ofstr1intostr2.Finally,
printf()displays both the original and copied strings.
व्याख्या:
stdio.hहेडर फाइल का उपयोग Input एवं Output Operations के लिए किया जाता है।string.hहेडर फाइल कोstrcpy()एवंstrcspn()फंक्शन्स के उपयोग के लिए शामिल किया गया है।दो Character Arrays
str1[255]एवंstr2[255]Declare किए गए हैं।fgets()Function का उपयोग Keyboard से String Input करने के लिए किया गया है।strcspn()Function Input String से Newline Character को हटाता है।strcpy(str2, str1)Statement की सहायता सेstr1की String कोstr2में Copy किया जाता है।अंत में
printf()Function Original एवं Copied Strings को Display करता है।
Important Points to Remember
strcpy()is a standard string handling function in C.It is declared in the
string.hheader file.It copies the source string into the destination string.
It also copies the null character (
'\0').The destination array must have sufficient memory.
The original source string remains unchanged.
The source and destination strings must not overlap.
महत्वपूर्ण बिंदु:
strcpy()C भाषा का एक Standard String Handling Function है।यह
string.hHeader File में Declare किया गया है।यह Source String को Destination String में Copy करता है।
यह Null Character (
'\0') को भी Copy करता है।Destination Array में पर्याप्त Memory होनी चाहिए।
Original Source String में कोई परिवर्तन नहीं होता है।
Source एवं Destination Strings के Memory Locations एक-दूसरे से Overlap नहीं होने चाहिए।
Comments
Post a Comment