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 World

C 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: Hello
Enter second string: World
Concatenated String: HelloWorld

Explanation

  1. str1 and str2 are declared to store two strings.
    str1 और str2 में दो Strings स्टोर की जाती हैं।

  2. fgets() accepts strings from the user.
    fgets() यूजर से Strings इनपुट लेता है।

  3. strcspn() removes the newline character.
    strcspn() Newline Character को हटाता है।

  4. strcat(str1, str2) appends str2 to the end of str1.
    यह 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

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