← Back to C Course | Chapter 12: Advanced Topics | Lesson 9 of 20

C की string.h Functions

string.h library text के लिए C का toolbox है, strings copy, join, compare, measure और search करने के tools के साथ। यह आपको वे loops खुद लिखने से बचाता है।
Syntax
c
#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()

c
// 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;
}

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()

c
// 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;
}

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()

c
// 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;
}

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()

c
// 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;
}

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()

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक बहुत छोटे buffer में strcpy इस्तेमाल करना, buffer overflow cause करते हुए।
  2. strncpy इस्तेमाल करना और यह भूल जाना कि यह null terminator add न करे।
  3. strings को strcmp की बजाय == से compare करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.