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

string.h એ loops આપે છે જે નહીંતર તમે જાતે લખતા: strlen '\0' સુધી ગણે છે, strcpy copy કરે છે (= થી strings assign થતી નથી), strcat જોડે છે, strrev ઉલટાવે છે, અને strcmp બરાબર માટે 0 આપે છે, સૌથી મોટો trap.

11 min read · 10 cards · 3 checks

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


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 માં copyd માં જગ્યા જોઈએ; 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 કરે છે?

  1. 0, કારણ કે strings બરાબર છે
  2. 1, એટલે true, એ match થાય છે
  3. 4, match ની લંબાઈ
  4. -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"); શું ખોટું છે?

  1. a માં જગ્યા નથી: 12 characters + '\0' 6 boxes માં સમાઈ શકતા નથી, memory overrun થાય છે
  2. કંઈ નહીં: strcat array ને resize કરી દે છે
  3. strcat, string literals ને જોડી શકતું નથી
  4. " 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 માં એ જણાવો.
  • યાદ રાખવાની યુક્તિ: 0 તફાવત એટલે બરાબર; વાડ સુધી ગણો.

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