C Program to Concatenate Two Strings Using strcat() (Function strcat() Function in C Language
The strcat() function in C is used to join two strings. It appends the second string at the end of the first string. The strcat() function is declared in the <string.h> header file.
C भाषा में strcat() फंक्शन का उपयोग दो Strings को जोड़ने के लिए किया जाता है। यह दूसरी String को पहली String के अंत में जोड़ता है।
Syntax
strcat(destination, source);Example:
char str1[20] = "Hello ";char str2[] = "World";strcat(str1, str2);printf("%s", str1);
Output:
Hello WorldC Program Using strcat() Function
The following program accepts two strings and concatenates them using strcat().
निम्नलिखित प्रोग्राम दो Strings इनपुट लेकर strcat() फंक्शन की सहायता से उन्हें जोड़ता है।
#include <stdio.h>#include <string.h>int main(){char str1[100], str2[50];printf("Enter first string: ");fgets(str1, sizeof(str1), stdin);printf("Enter second string: ");fgets(str2, sizeof(str2), stdin);str1[strcspn(str1, "\n")] = '\0';str2[strcspn(str2, "\n")] = '\0';strcat(str1, str2);printf("Concatenated String: %s", str1);return 0;}
Output
Enter first string: HelloEnter second string: WorldConcatenated String: HelloWorld
Explanation
str1andstr2are declared to store two strings.str1औरstr2में दो Strings स्टोर की जाती हैं।fgets()accepts strings from the user.fgets()यूजर से Strings इनपुट लेता है।strcspn()removes the newline character.strcspn()Newline Character को हटाता है।strcat(str1, str2)appendsstr2to the end ofstr1.यहstr2कोstr1के अंत में जोड़ता है।
Important Points
strcat()is defined in<string.h>.It joins two strings.
The destination array must have enough memory for the combined string, including
'\0'.The source string remains unchanged.
महत्वपूर्ण बिंदु:
strcat()फंक्शन<string.h>में Defined है।यह दो Strings को जोड़ता है।
Destination Array में पूरी String के लिए पर्याप्त Memory होनी चाहिए।
Source String में कोई बदलाव नहीं होता।
Comments
Post a Comment