समस्या
strncat लाइब्रेरी फंक्शन का उपयोग करके सोर्स स्ट्रिंग से n कैरेक्टर्स को डेस्टिनेशन स्ट्रिंग में जोड़ने के लिए C प्रोग्राम लिखें।
समाधान
strcat फंक्शन
यह फंक्शन दो स्ट्रिंग्स को जोड़ने या संयोजित करने के लिए उपयोग किया जाता है।
डेस्टिनेशन स्ट्रिंग की लंबाई सोर्स स्ट्रिंग से अधिक होनी चाहिए।
परिणामी संयोजित स्ट्रिंग सोर्स स्ट्रिंग में होगी।
सिंटैक्स
strcat(Destination String, Source string);
उदाहरण 1
#include <string.h>
main(){
char a[50] = "Hello";
char b[20] = "Good Morning";
clrscr();
strcat(a, b);
printf("concatenated string = %s", a);
getch();
}
आउटपुट
Concatenated string = Hello Good Morning
strncat फंक्शन
यह फंक्शन एक स्ट्रिंग के n कैरेक्टर्स को दूसरी स्ट्रिंग में जोड़ने के लिए उपयोग किया जाता है।
डेस्टिनेशन स्ट्रिंग की लंबाई सोर्स स्ट्रिंग से अधिक होनी चाहिए।
परिणामी संयोजित स्ट्रिंग डेस्टिनेशन स्ट्रिंग में होगी।
सिंटैक्स
strncat(Destination String, Source string, n);
उदाहरण 2
#include <string.h>
main(){
char a[30] = "Hello";
char b[20] = "Good Morning";
clrscr();
strncat(a, b, 4);
a[9] = '\0';
printf("concatenated string = %s", a);
getch();
}
आउटपुट
Concatenated string = Hello Good.
उदाहरण 3
#include<stdio.h>
#include<string.h>
void main(){
// सोर्स और डेस्टिनेशन स्ट्रिंग्स घोषित करना //
char source[45], destination[50];
// यूजर से सोर्स स्ट्रिंग और डेस्टिनेशन स्ट्रिंग पढ़ना //
printf("Enter the source string :");
gets(source);
printf("Enter the destination string before :");
gets(destination);
// उपरोक्त सभी परिणामों को संयोजित करना //
destination[2] = '\0';
strncat(destination, source, 2);
strncat(destination, &source[4], 1);
// डेस्टिनेशन स्ट्रिंग प्रिंट करना //
printf("The modified destination string :");
puts(destination);
}
आउटपुट
Enter the source string : TutorialPoint Enter the destination string before : C Programming The modified destination string : C Tur