C की string.h Functions
In this page:
#include <string.h>
strncpy(destination, source, size);
strncat(destination, source, size);
strcmp(string1, string2);
strcpy() और strncpy() से Strings Copy करना
string.h के copy functions एक source string से character data को एक destination buffer में duplicate करते हैं। strcpy() बिना किसी length check के null terminator तक copy करता है, जबकि strncpy() एक explicit maximum character count लेता है, इसे जब भी destination buffer source से छोटा हो सकता है तो safer choice बनाते हुए।
उदाहरण: Copying Strings with strcpy() and strncpy()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
char dest[20];
strncpy(dest, "Hello", 20);
// Print formatted text to the screen
printf("%s", dest);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
strcat() और strncat() से Strings जोड़ना
strcat() एक source string के characters को एक destination string के अंत में append करता है, destination के existing null terminator को overwrite करते हुए और appended text के बाद एक नया add करते हुए। strncat() append करने वाले characters की एक maximum संख्या लेता है, जो एक source string को destination buffer overflow करने से रोकने के लिए essential है।
उदाहरण: Concatenating Strings with strcat() and strncat()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
char str[20] = "Hello";
strncat(str, " World", 6);
// Print formatted text to the screen
printf("%s", str);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
strcmp() और strncmp() से Strings Compare करना
strcmp() दो strings को character by character lexicographic (dictionary) order में compare करता है, सिर्फ तभी 0 return करता है जब वे exactly identical हों, पहली string पहले sort होने पर एक negative value, और बाद में sort होने पर एक positive value। strncmp() same comparison करता है लेकिन एक specified संख्या characters के बाद रुक जाता है, सिर्फ एक prefix compare करने के लिए useful।
उदाहरण: Comparing Strings with strcmp() and strncmp()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", strcmp("apple", "apple"));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
strlen() से Lengths ढूंढना
strlen() एक string को terminating null byte मिलने तक चलता है और इससे पहले characters की count return करता है, जिसमें वह null terminator खुद शामिल नहीं है।
यही कारण है कि एक n-character string रखने वाले buffer को actually कम से कम n+1 bytes का होना चाहिए ताकि terminator के लिए जगह बचे जिसे strlen() count नहीं करता।
उदाहरण: Finding Lengths with strlen()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%zu", strlen("Hello"));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
strchr() और strstr() से Strings में Search करना
strchr() एक single character के पहले occurrence के लिए एक string scan करता है और इसका एक pointer return करता है (या न मिलने पर NULL), जबकि strstr() same तरह का search करता है लेकिन सिर्फ एक character की बजाय एक पूरी substring ढूंढता है।
एक manual search loop लिखे बिना एक बड़ी C string के अंदर text locate करने के लिए दोनों standard tools हैं।
उदाहरण: Searching Strings with strchr() and strstr()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
char *result = strchr("Hello", 'e');
// Print formatted text to the screen
printf("%s", result);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक बहुत छोटे buffer में
strcpyइस्तेमाल करना, buffer overflow cause करते हुए। strncpyइस्तेमाल करना और यह भूल जाना कि यह null terminator add न करे।- strings को
strcmpकी बजाय==से compare करना।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions