C के String Functions
In this page:
#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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Strings जोड़ना (strcat)
strcat() में strcpy() जैसा ही overflow risk है क्योंकि यह check नहीं करता कि destination buffer में appended text के लिए काफी बची space है -- हमेशा पक्का करें कि destination दोनों strings combined रखने के लिए काफी बड़ा allocate किया गया था।
उदाहरण: Concatenating Strings (strcat)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Strings Compare करना (strcmp)
यह return convention दिखाता है कि strcmp internally alphabetical ordering को एक बार में एक pair character codes compare करके कैसे mimic करता है, इसलिए apple banana से कम compare होता है क्योंकि a का character code b से छोटा है।
उदाहरण: Comparing Strings (strcmp)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक बहुत छोटे buffer में
strcpyइस्तेमाल करना, buffer overflow cause करते हुए। - strings को
strcmpकी बजाय==से compare करना। strlenयाstrcpyइस्तेमाल करने से पहले#include <string.h>भूल जाना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: