सरल उदाहरणों के साथ समझें
C प्रोग्रामिंग में, string functions वह pre-defined functions होते हैं जो strings (character arrays) पर विभिन्न operations करने के लिए उपयोग किए जाते हैं। ये functions string.h
header file में उपलब्ध होते हैं।
यहाँ कुछ सामान्य string functions और उनके उदाहरण दिए गए हैं:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
int length = strlen(str);
printf("Length of string: %d", length);
return 0;
}
Output: Length of string: 13
व्याख्या: इस function से हम string की लंबाई प्राप्त करते हैं, जिसमें null character '\0' शामिल नहीं होता।
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello";
char destination[20];
strcpy(destination, source);
printf("Copied string: %s", destination);
return 0;
}
Output: Copied string: Hello
व्याख्या: strcpy()
function source string को destination string में copy करता है।
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = " World!";
strcat(str1, str2);
printf("Concatenated string: %s", str1);
return 0;
}
Output: Concatenated string: Hello World!
व्याख्या: strcat()
function str2 को str1 के अंत में जोड़ता है।
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Apple";
char str2[] = "Banana";
int result = strcmp(str1, str2);
if(result == 0) {
printf("Strings are equal");
} else if(result < 0) {
printf("str1 is less than str2");
} else {
printf("str1 is greater than str2");
}
return 0;
}
Output: str1 is less than str2
व्याख्या: strcmp()
function दो strings की lexicographical (dictionary) तुलना करता है।
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *ptr = strchr(str, 'o');
if(ptr != NULL) {
printf("Character found at position: %ld", ptr - str);
} else {
printf("Character not found");
}
return 0;
}
Output: Character found at position: 4
व्याख्या: strchr()
function string में किसी character की पहली occurrence को ढूंढता है।
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *ptr = strstr(str, "World");
if(ptr != NULL) {
printf("Substring found at position: %ld", ptr - str);
} else {
printf("Substring not found");
}
return 0;
}
Output: Substring found at position: 7
व्याख्या: strstr()
function string में किसी substring की पहली occurrence को ढूंढता है।
C प्रोग्रामिंग में string functions का उपयोग करके हम strings पर विभिन्न operations जैसे length प्राप्त करना, copy करना, जोड़ना, तुलना करना, और खोज करना आदि कर सकते हैं। ये functions string.h
header file में उपलब्ध होते हैं और इनका उपयोग करके हम अपने प्रोग्राम्स को अधिक प्रभावी और उपयोगकर्ता-मित्र बना सकते हैं।