Theory
Match કે નહીં?
string.h નું strcmp 2 strings ની સરખામણી કરે છે:
char name[10] = "Riya";
if (strcmp(name, "Riya")) printf("Match");
else printf("No match");
શું print થાય છે?
Think first
પહેલાં તમારો અંદાજ
એ શું print કરે છે તે type કરો.
Show the answer
No match. strcmp તફાવત માપે છે: સરખી strings 0 આપે છે, અને 0 એટલે false. Strings સરખી છે કે નહીં તે if (strcmp(a, b) == 0) થી ચકાસો; સાદું if (strcmp(a, b)) ત્યારે true થાય છે જ્યારે strings અલગ હોય.
Theory
વાડ
string.h ના આ functions તૈયાર loops છે, જે string ની વાડ સુધી, એટલે કે એના null terminator '\0' સુધી, ચાલે છે: strlen વાડ પહેલાંના characters ગણે છે.
Practical
strlen કે sizeof?
#include <stdio.h>
#include <string.h>
int main() {
char city[20] = "Surat";
int len = strlen(city);
int size = sizeof(city);
printf("%d %d", len, size);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
ઉપરનો program શું print કરે છે?
- 5 20
- 6 20
- 5 6
- 5 5
Show the answer
5 20
strlen વાડ પર અટકી જાય છે, એને ગણ્યા વગર: 5. sizeof array ની capacity આપે છે: 20 bytes, અંદર ગમે તે રાખેલું હોય. 6 20 વાડને પણ ગણી લે છે.
Think first
ભૂલ શોધો
char name[10];
name = "Riya";
Line 2 compile કેમ નહીં થાય? એને સુધારો.
Show the answer
Array નું નામ એક constant address છે: box zero પર રોપેલો signpost, અને signposts ખસતા નથી. એને બદલે copy કરો: strcpy(name, "Riya");, જેમાં = ની જેમ destination પહેલાં આવે છે. Declaration વખતે જ initialize કરવામાં કોઈ વાંધો નથી.
Think first
પૂરતી જગ્યા છે?
strcat(dest, src), src ને dest ના '\0' ની જગ્યાએથી જોડે છે: Unit 3 વાળું તમારું merge, હવે તૈયાર મળે છે.
char full[8] = "Riya";
strcat(full, " Shah");
શું ખોટું થાય છે?
Show the answer
જગ્યા નથી: Riya Shah અને '\0' માટે 10 boxes જોઈએ; full માં 8 છે. Program compile તો થાય છે, પણ strcat array ના છેડાથી આગળ લખે છે (C ક્યારેય arrays ને resize કરતું નથી), અને memory corrupt કરે છે.
Think first
Standard C છે?
strrev(s), s ને reverse કરે છે: "Riya" નું "ayiR" થાય છે. શું એ standard C છે?
Show the answer
ના. conio.h ની જેમ એ Turbo C માં મળે છે, પણ એ non-standard છે. Linux ના gcc પર reverse loop જાતે લખો.
Theory
કઈ string પહેલાં આવે?
strcmp(a, b) character code પ્રમાણે, પહેલો તફાવત મળે ત્યાં સુધી, character by character સરખામણી કરે છે: a પહેલાં આવે તો negative, b પહેલાં આવે તો positive. ફક્ત sign ની જ ખાતરી છે.
Practical
Exam-style: output કહો
#include <stdio.h>
#include <string.h>
int main() {
char a[20] = "Riya";
char b[20] = "Rohan";
char first[20];
int n;
strcat(a, " Shah");
if (strcmp(a, b) < 0)
strcpy(first, a);
else
strcpy(first, b);
n = strlen(first);
printf("%s %d", first, n);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
Exam-style: ઉપરનો program શું print કરે છે?
- Riya Shah 9
- Riya Shah 10
- Rohan 5
- Riya Shah 8
Show the answer
Riya Shah 9
a બદલાઈને Riya Shah બને છે. બંનેના R પછી 'i', 'o' ની પહેલાં આવે છે, એટલે strcmp negative છે અને first માં a copy થાય છે: 9 characters, space સહિત. Rohan 5 લંબાઈની સરખામણી કરે છે કે sign ને ઉલટો વાંચે છે; Riya Shah 10, '\0' ને પણ ગણી લે છે.
Summary
Key takeaways
- strcmp સરખી strings માટે 0 આપે છે, ક્રમ માટે negative કે positive; == 0 થી ચકાસો.
- strlen '\0' ની પહેલાં અટકે છે; sizeof capacity આપે છે.
- Arrays = ની ના પાડે છે: strcpy copy કરે છે, strcat જોડે છે; destination પહેલાં આવે અને એમાં પૂરતી જગ્યા જોઈએ; strrev non-standard છે.
- યાદ રાખવાની યુક્તિ: વાડ સુધી ચાલો; તફાવત 0 એટલે strings સરખી.