← Back to C Course | Chapter 6: Arrays & Strings | Lesson 7 of 8

C के String Functions

String functions text के लिए ready-made tools हैं, जैसे measure करना, copy करना, जोड़ना, और compare करना। ये आपको हर letter में खुद loop करने से बचाते हैं।
Syntax
c
#include <string.h>

strlen(string_name);
strcpy(destination, source);
strcat(destination, source);
strcmp(string1, string2);

String Length ढूंढना (strlen)

strlen() को string को character by character scan करना होता है जब तक यह null terminator न ढूंढ ले, इसे एक O(n) operation बनाते हुए -- एक loop condition के अंदर इसे बार-बार call करना (जैसे for (i=0; i<strlen(s); i++)) हर iteration पर चुपचाप पूरी string दोबारा scan करता है।

उदाहरण: Finding String Length (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() {
	char str[] = "Hello";
	// Print formatted text to the screen
	printf("%zu", strlen(str));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Strings Copy करना (strcpy)

strcpy() destination buffer पर कोई bounds checking नहीं करता, इसलिए अगर source string destination array से लंबी हो, यह adjacent memory में overflow हो जाता है -- एक explicit length limit के साथ strncpy() ज़्यादातर modern code में safer alternative है।

उदाहरण: Copying Strings (strcpy)

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];
	strcpy(dest, "Hello");
	// Print formatted text to the screen
	printf("%s", dest);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Strings जोड़ना (strcat)

strcat() में strcpy() जैसा ही overflow risk है क्योंकि यह check नहीं करता कि destination buffer में appended text के लिए काफी बची space है -- हमेशा पक्का करें कि destination दोनों strings combined रखने के लिए काफी बड़ा allocate किया गया था।

उदाहरण: Concatenating Strings (strcat)

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 ";
	strcat(str, "World");
	// Print formatted text to the screen
	printf("%s", str);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Strings Compare करना (strcmp)

यह return convention दिखाता है कि strcmp internally alphabetical ordering को एक बार में एक pair character codes compare करके कैसे mimic करता है, इसलिए apple banana से कम compare होता है क्योंकि a का character code b से छोटा है।

उदाहरण: Comparing Strings (strcmp)

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", "banana"));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Strings में Search करना (strstr)

strstr() case-sensitive है, इसलिए "concatenate" के अंदर "Cat" खोजना NULL return करता है भले ही "cat" इसमें appear करता हो -- यह इतनी बार लोगों को confuse करता है कि case-insensitive searches को आमतौर पर एक custom loop या strcasestr() जैसे platform-specific function की ज़रूरत होती है।

उदाहरण: Searching Strings (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 = strstr("concatenate", "cat");
	// 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. strings को strcmp की बजाय == से compare करना।
  3. strlen या strcpy इस्तेमाल करने से पहले #include <string.h> भूल जाना।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

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.