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

string.h functions walk a string to its '\0', and strcmp returns 0 for equal strings.

5 min read · 12 cards · 6 checks

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


Theory

Match or not?

strcmp, from string.h, compares two strings:

char name[10] = "Riya";

if (strcmp(name, "Riya")) printf("Match");

else printf("No match");

What prints?

Think first

Your guess first

Type what it prints.

Show the answer

No match. strcmp measures difference: equal strings give 0, and 0 is false. Test equality with if (strcmp(a, b) == 0); plain if (strcmp(a, b)) is true when they differ.

Theory

The fence

These string.h functions are ready-made loops walking to a string's fence, its null terminator '\0': strlen counts the characters before it.

Practical

strlen or 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

What does the program above print?

  1. 5 20
  2. 6 20
  3. 5 6
  4. 5 5
Show the answer

5 20

strlen stops at the fence without counting it: 5. sizeof gives the array's capacity: 20 bytes, whatever is stored. 6 20 counts the fence.

Think first

Spot the error

char name[10];

name = "Riya";

Why will line 2 not compile? Fix it.

Show the answer

An array name is a constant address, a signpost at box zero, and signposts do not move. Copy instead: strcpy(name, "Riya");, destination first, like =. Initializing at declaration is fine.

Think first

Room enough?

strcat(dest, src) appends src at dest's '\0': your Unit 3 merge, ready-made.

char full[8] = "Riya";

strcat(full, " Shah");

What goes wrong?

Show the answer

No room: Riya Shah plus '\0' needs 10 boxes; full has 8. It compiles, but strcat writes past the end (C never resizes arrays), corrupting memory.

Think first

Standard C?

strrev(s) reverses s: "Riya" becomes "ayiR". Is it standard C?

Show the answer

No. Turbo C has it, like conio.h, but it is non-standard. On Linux gcc, write the reverse loop yourself.

Theory

Which comes first?

strcmp(a, b) compares character by character, by character code, to the first difference: negative if a comes first, positive if b does. Only the sign is promised.

Practical

Exam-style: predict the 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: what does the program above print?

  1. Riya Shah 9
  2. Riya Shah 10
  3. Rohan 5
  4. Riya Shah 8
Show the answer

Riya Shah 9

a becomes Riya Shah. After the R's, 'i' comes before 'o', so strcmp is negative and first gets a: 9 characters, space included. Rohan 5 compares lengths or flips the sign; Riya Shah 10 counts the '\0'.

Summary

Key takeaways

  • strcmp gives 0 when EQUAL, negative or positive for order; test == 0.
  • strlen stops before the '\0'; sizeof gives the capacity.
  • Arrays refuse =: strcpy copies, strcat appends, destination first, with room; strrev is non-standard.
  • Memory hook: walk to the fence; zero difference means equal.

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