Built-in String functions: strlen, strcmp, strcpy, strcat, strrev

string.h ના functions string માં એના '\0' સુધી ચાલે છે, અને strcmp સરખી strings માટે 0 return કરે છે.

5 min read · 12 cards · 6 checks

Read in: English · हिन्दी · ગુજરાતી


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 કરે છે?

  1. 5 20
  2. 6 20
  3. 5 6
  4. 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 કરે છે?

  1. Riya Shah 9
  2. Riya Shah 10
  3. Rohan 5
  4. 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 સરખી.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Functions and Structure

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Built-in String functions: strlen, strcmp, strcpy, strcat, strrev · Computer Programming and Programming Methodology (CPPM) · Gri-Learn