Theory
जो loops अब आप नहीं लिखते
पिछले unit में आपने दो strings हाथ से merge कीं: '\0' तक चलो, आगे copy करो, फिर से terminate करो। ईमानदार मेहनत, और 30 साल पहले ही लिखी जा चुकी।
C उन loops को string.h में functions के रूप में देता है: नापना, copy करना, जोड़ना, तुलना करना, उलटना। Marks program को ये आज चाहिए, क्योंकि students के नाम होते हैं, और C में नाम तेज़ धार वाले char arrays हैं।
इन पाँच functions में से एक, C का सबसे ग़लत पढ़ा जाने वाला return value रखता है। वह तुलना वाला है, ज़ाहिर है।
At a glance
string.h की पाँच
| Function | काम | ध्यान दें |
|---|---|---|
| strlen(s) | characters गिनना | '\0' को छोड़ता है |
| strcpy(d, s) | s को d में copy | d में जगह चाहिए; d पहले |
| strcat(d, s) | s को d पर जोड़ना | d के '\0' पर जुड़ता है; फिर जगह |
| strcmp(a, b) | alphabetically तुलना | बराबर होने पर 0 देता है |
| strrev(s) | जगह पर उलटना | Non-standard (Turbo C) |
Theory
strcpy आख़िर है ही क्यों
नए students लिखते हैं:
char name[10];
name = "Riya"; /* COMPILE ERROR */
यह एक वजह से नाकाम होता है जो अब आपकी अपनी है: name एक constant address है (गड़ा हुआ signpost, पिछला lesson), और = किसी signpost को दोबारा target नहीं कर सकता। CONTENTS को boxes में copy करना एक loop का काम है, और strcpy वही loop है:
strcpy(name, "Riya"); /* destination first, like d = s */
Argument का क्रम assignment जैसा है: destination, फिर source। strcat के लिए भी वही।
Practical
नाम, marks program से मिलते हैं
#include <stdio.h>
#include <string.h>
int main() {
char first[20] = "Riya";
char full[40];
strcpy(full, first); /* full: "Riya" */
strcat(full, " Shah"); /* full: "Riya Shah" */
printf("%s (%lu letters)\n", full, strlen(full));
if (strcmp(full, "Riya Shah") == 0)
printf("Names match\n");
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
strcmp("Riya", "Riya") क्या return करता है?
- 0, क्योंकि strings बराबर हैं
- 1, यानी true, वे match करती हैं
- 4, match की लंबाई
- -1, strings की कभी तुलना नहीं हो सकती
Show the answer
0, क्योंकि strings बराबर हैं
strcmp जवाब देता है "ये कैसे अलग हैं?": 0 का मतलब कोई अंतर नहीं, negative का मतलब पहली alphabetically पहले आती है, positive का मतलब बाद में। इसलिए बराबरी की जाँच if (strcmp(a, b) == 0) लिखी जाती है। strcmp को हाँ/ना match function समझना हर नतीजे को उलट देता है, और if (strcmp(a, b)) दरअसल तब true होता है जब strings अलग हों। यही THE string-function वाला exam trap है।
Think first
strlen की तरह गिनिए
char city[20] = "Surat"; strlen(city) क्या return करता है, और यह 6 क्यों नहीं है (आपने पिछले किसी lesson में '\0' गिना था) और 20 क्यों नहीं?
Show the answer
5. strlen boxes में चलकर letters गिनता है और '\0' पर रुक जाता है, बिना उसे गिने: S-u-r-a-t, पाँच। यह 6 नहीं है क्योंकि terminator एक बाड़ है, letter नहीं; 20 नहीं क्योंकि array की capacity sizeof का काम है, strlen का नहीं। याद रखने की एक line: strlen बाड़ तक गिनता है; sizeof पूरा plot नापता है।
Quiz
char a[6] = "Hello"; strcat(a, " World"); क्या गलत है?
- a में जगह नहीं: 12 characters + '\0' 6 boxes में नहीं समा सकते, memory overrun होती है
- कुछ नहीं: strcat array को resize कर देता है
- strcat, string literals को नहीं जोड़ सकता
- " World" में जो space है वह गैर-कानूनी है
Show the answer
a में जगह नहीं: 12 characters + '\0' 6 boxes में नहीं समा सकते, memory overrun होती है
a भरा हुआ है (5 letters + '\0'), और strcat खुशी-खुशी " World" को उसके अंत के आगे लिख देता है, out-of-bounds corruption, फिर वही gets() वाली बीमारी। C के arrays कभी resize नहीं होते; DESTINATION को combined text और terminator के लिए काफी बड़ा declare करना ज़रूरी है। "इस strcat में क्या गलत है" lab-exam का नियमित सवाल है।
Watch out
Marks कहाँ कटते हैं
strcmp का ग़लत इस्तेमाल (0 का मतलब बराबर, ऊपर)। name = "Riya": गैर-कानूनी; strcpy इसी के लिए है। Argument का क्रम: strcpy/strcat में destination पहले, arguments उलटने पर copy गलत दिशा में होती है। strlen बनाम sizeof एक ही सवाल में, बाड़-बनाम-plot वाली line तैयार रखिए। strrev: इसे non-standard (Turbo C) बताइए, ठीक getch की तरह; GCC पर reverse loop आप खुद लिखते हैं।
Theory
आप पाँचों लिख सकते हैं
यह रहा आत्मविश्वास बढ़ाने वाला सच: pointers और loops के साथ, आप इनमें से हर एक को चार lines में बना सकते हैं, strlen '\0' तक p++ है, और lab में एक बार ऐसा करना तीन revisions के बराबर है। अगला lesson: numbers के लिए वही ready-made इलाज, math.h, जहाँ floor और round एक function होने से इनकार करते हैं।
Summary
Key takeaways
- string.h, string loops देता है: strlen, strcpy, strcat, strcmp, strrev।
- strlen '\0' तक गिनता है, उसे छोड़कर; sizeof capacity नापता है।
- Arrays = से इनकार करते हैं: strcpy(dest, src) copy करता है, destination पहले।
- strcat '\0' पर जोड़ता है; destination में जगह होनी चाहिए, C कभी resize नहीं करता।
- strcmp बराबर के लिए 0 देता है (क्रम के लिए negative/positive), == 0 से जाँचिए।
- strrev non-standard है (Turbo C), exams में यह बता दीजिए।
- याद रखने का hook: 0 अंतर का मतलब बराबर; बाड़ तक गिनिए।