Library functions Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

The ideas behind the lab

Exactly what you watched happen, in plain steps.

Loops you do not have to write

string.h holds functions that work on whole strings — they all walk the characters until '\0'. ctype.h holds functions that test or convert one character. None of the string functions know how big your arrays are: the destination must have enough room, or memory next to it is overwritten. That is why the "n" versions (strncpy, strncat) and snprintf, which take a size, exist.

#include <string.h>
#include <ctype.h>

char d[20];
strcpy(d, "Hello");          // d = "Hello"
strcat(d, ", C");           // d = "Hello, C"
strlen(d)                    // 8
strcmp(d, "Hello")           // > 0 (d is longer)
toupper('a')                 // 'A'
isdigit('7')                 // true (non-zero)
The most used functions
Function What it does Result / note
strlen(s) Counts characters before '\0' size_t; '\0' not counted
strcpy(d, s) Copies s (with '\0') into d d must be big enough
strncpy(d, s, n) Copies at most n chars May leave d without '\0'
strcat(d, s) Appends s to the end of d d needs room for both
strcmp(a, b) Compares character codes < 0, 0 or > 0
strchr(s, c) / strstr(s, t) Finds a char / a substring Pointer to it, or NULL
isalpha, isdigit, isspace, isupper Tests one character Non-zero = true
toupper(c) / tolower(c) Converts one letter Other chars unchanged

Copying and joining

  1. strcpy(d, s) copies every character of s, then '\0'.
  2. strcat(d, s) first finds the '\0' of d, then copies s there.
  3. The destination comes first, like an assignment: d = s.
  4. Neither checks the size of d: too little room = buffer overflow.
  5. Safer: snprintf(d, sizeof d, "%s%s", a, b) never writes past sizeof d.

Comparing

  1. strcmp walks both strings until two characters differ or both end.
  2. It returns 0 if equal, negative if a comes first, positive if b does.
  3. Test with == 0, < 0, > 0 — not with == -1 or == 1.
  4. It compares codes: all uppercase letters come before lowercase.
  5. For "same ignoring case", convert both with tolower first.

Characters with ctype.h

  1. isdigit(c): '0'–'9'; isalpha(c): a letter; isalnum(c): either.
  2. isspace(c): space, tab, newline; isupper / islower: the case.
  3. toupper(c) and tolower(c) return the converted char — store it: s[i] = toupper(s[i]).
  4. They take an int: pass (unsigned char) c for characters above 127.
  5. A digit's value: c - '0' ('7' - '0' is 7).
Walkthrough

Your own strlen and strcpy, then a palindrome test

Every line, explained.

1 #include <stdio.h>
2 #include <string.h>
3 #include <ctype.h>
4
5 int myStrlen(const char s[]) const: the function only reads s.
6 {
7 int n = 0;
8 while (s[n] != '\0') Exactly what strlen does.
9 n++;
10 return n;
11 }
12
13 void myStrcpy(char dest[], const char src[])
14 {
15 int i = 0;
16 while ((dest[i] = src[i]) != '\0') Copy, then test: the '\0' is copied too.
17 i++;
18 }
19
20 int main(void)
21 {
22 char word[20], copy[20];
23 myStrcpy(word, "Level");
24 printf("%d\n", myStrlen(word)); Prints 5
25 strcpy(copy, word); The library version.
26 for (int i = 0; copy[i]; i++) copy[i] is 0 (false) at the terminator.
27 copy[i] = tolower(copy[i]); copy = "level"
28 int n = strlen(copy), pal = 1;
29 for (int i = 0; i < n / 2; i++) Compare from both ends.
30 if (copy[i] != copy[n - 1 - i]) pal = 0;
31 printf("%s\n", pal ? "palindrome" : "not a palindrome"); Prints palindrome
32 return 0;
33 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

Destination too small

✗ Wrong
char d[6] = "Hi, ";
strcat(d, "World");   // needs 10 bytes
✓ Right
char d[16] = "Hi, ";
strcat(d, "World");

strcat writes past the end of d — a buffer overflow that silently corrupts other memory.

Arguments in the wrong order

✗ Wrong
strcpy("Hello", d);   // copies INTO the literal
✓ Right
strcpy(d, "Hello");   // destination first

The first argument is always the destination. Writing into a string literal crashes.

Testing strcmp as true / false

✗ Wrong
if (strcmp(a, b))   // runs when they DIFFER
✓ Right
if (strcmp(a, b) == 0)   // equal

strcmp returns 0 for equal strings, and 0 means false.

Forgetting to store toupper's result

✗ Wrong
toupper(s[i]);          // s is unchanged
✓ Right
s[i] = toupper(s[i]);

toupper returns the new character; it cannot change its argument.

strncpy without a terminator

✗ Wrong
char d[4];
strncpy(d, "Hello", 4);   // H e l l — no \0
✓ Right
char d[4];
strncpy(d, "Hello", 3);
d[3] = '\0';

When the source is too long, strncpy does not add '\0'. Add it yourself.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

strlen("hello") returns?

Question 2

char d[20] = "ab"; strcat(d, "cd"); — what is d?

Question 3

What does strcmp("cat", "cat") return?

Question 4

Is strcmp("Zebra", "apple") negative or positive?

Question 5

What is '8' - '0'?

Assessment

String & Character Functions quiz

Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.