Learn C Programming

Lesson 4 of 9 · Strings

Module 10 · Strings

string.h: strlen, strcpy, strcmp, strcat

Free with an accountReading

Zara wants her program to say when one word sorts after another. She tried strcmp on two words written into the code, and it printed 1. So her test became if (strcmp(a, b) == 1). Then she read the words from the input, and the test never fired again.

Sign in to read

This lesson is free, with an account

An account costs nothing and opens every lesson in this track that is not marked Pro, on every device you read on.

What is inside string.h: strlen, strcpy, strcmp, strcat

  1. Zara's test that never fires
  2. Reading a string.h prototype
  3. strlen walks the whole string, every call
  4. strcpy needs room for the text and its zero
  5. strncpy is not the safe strcpy
  6. strcat joins, after you check the room
  7. strcmp walks to the first difference
  8. strchr and strstr answer yes or no