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

string.h ships the loops you would otherwise write: strlen counts to '\0', strcpy copies (you cannot assign strings with =), strcat joins, strrev reverses, and strcmp returns 0 for EQUAL, the trap of traps.

11 min read · 10 cards · 3 checks

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


Theory

The loops you no longer write

Last unit you merged two strings by hand: walk to the '\0', copy on, re-terminate. Honest work, and already written thirty years ago.

C ships those loops as functions in string.h: measuring, copying, joining, comparing, reversing. The marks program needs them TODAY, because students have names, and names in C are char arrays with sharp edges.

One of these five functions carries the most misread return value in C. It is the comparison one, naturally.

At a glance

The string.h five

FunctionJobWatch out
strlen(s)Count charactersExcludes the '\0'
strcpy(d, s)Copy s INTO dd needs enough room; d first
strcat(d, s)Append s onto dJoins at d's '\0'; room again
strcmp(a, b)Compare alphabeticallyReturns 0 when EQUAL
strrev(s)Reverse in placeNon-standard (Turbo C)

Theory

Why strcpy exists at all

New students write:

char name[10];

name = "Riya"; /* COMPILE ERROR */

It fails for a reason you now own: name is a constant address (a planted signpost, last lesson), and = cannot re-aim a signpost. Copying CONTENTS into the boxes is a loop's job, and strcpy IS that loop:

strcpy(name, "Riya"); /* destination first, like d = s */

Argument order mirrors assignment: destination, then source. Same for strcat.

Practical

Names meet the 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") returns what?

  1. 0, because the strings are EQUAL
  2. 1, meaning true, they match
  3. 4, the length of the match
  4. -1, strings can never be compared
Show the answer

0, because the strings are EQUAL

strcmp answers "how do these differ?": 0 means no difference, negative means the first comes earlier alphabetically, positive means later. So equality tests read if (strcmp(a, b) == 0). Treating strcmp like a yes/no match function inverts every result, and if (strcmp(a, b)) is actually true when the strings DIFFER. This is THE string-function exam trap.

Think first

Count like strlen

char city[20] = "Surat"; What does strlen(city) return, and why is it not 6 (you counted the '\0' in an earlier lesson) and not 20?

Show the answer

5. strlen walks the boxes counting letters and STOPS AT '\0' without counting it: S-u-r-a-t, five. It is not 6 because the terminator is a fence, not a letter; not 20 because array capacity is sizeof's business, not strlen's. One line to remember: strlen counts up to the fence; sizeof measures the whole plot.

Quiz

char a[6] = "Hello"; strcat(a, " World"); What is wrong?

  1. a has no room: 12 characters + '\0' cannot fit in 6 boxes, memory is overrun
  2. Nothing: strcat resizes the array
  3. strcat cannot join string literals
  4. The space in " World" is illegal
Show the answer

a has no room: 12 characters + '\0' cannot fit in 6 boxes, memory is overrun

a is full (5 letters + '\0'), and strcat happily writes " World" past its end, out-of-bounds corruption, the gets() disease again. C arrays never resize; the DESTINATION must be declared big enough for the combined text plus terminator. "What is wrong with this strcat" is a lab-exam regular.

Watch out

Where marks leak

strcmp misuse (0 means equal, above). name = "Riya": illegal; strcpy exists precisely for this. Argument order: destination FIRST in strcpy/strcat, swapped arguments copy the wrong way. strlen vs sizeof in the same question, keep the fence-vs-plot line ready. strrev: mark it non-standard (Turbo C) exactly like getch; on GCC you write the reverse loop yourself.

Theory

You could write all five

Here is the confidence-builder: with pointers and loops, YOU can implement each of these in four lines, strlen is p++ until '\0', and doing it once in the lab is worth three revisions. Next lesson: the same ready-made treatment for numbers, math.h, where floor and round refuse to be the same function.

Summary

Key takeaways

  • string.h ships the string loops: strlen, strcpy, strcat, strcmp, strrev.
  • strlen counts to '\0', excluding it; sizeof measures capacity.
  • Arrays refuse =: strcpy(dest, src) copies, destination first.
  • strcat joins at the '\0'; destination must have room, C never resizes.
  • strcmp returns 0 for EQUAL (negative/positive for order), test with == 0.
  • strrev is non-standard (Turbo C), say so in exams.
  • Memory hook: 0 difference means equal; count to the fence.

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