Theory
Where does "Masala Dosa" live?
The till must store item names: "Chai", "Masala Dosa", "Chhaas".
Here is the surprise: core C++ inherited no built-in string type from C. There is int for 12, double for 12.50, and for text... nothing of that kind.
So how has every C program since 1972 handled text? With a trick: an army of chars standing in a line, plus one invisible guard marking where the line ends.
Theory
A train with a guard coach
Think of a string as a train: each coach carries one character, C, h, a, i. At the end rides a guard coach that passengers never see: the null character \0.
Every string function works the same way: start at the engine and walk coach by coach until the guard. That is how they know where the string stops. Lose the guard and the function walks into random memory.
Theory
Character array, formally
A C-style string is a char array whose content ends with the null terminator `\0`:
char item[6] = "Chai";
Memory: C h a i \0 (5 slots used, 1 spare). The literal "Chai" has 4 visible characters but needs 5 bytes.
Two rules: the array must be big enough for the text plus \0, and after declaration you cannot assign with = (item = "Dosa" is illegal, arrays are not assignable: copying is a function's job).
Theory
A pointer walks the train
A pointer to char holds the address of the first coach:
char *p = item;
Now *p is 'C', and p++ moves to the next character. This loop prints a string the way the library itself would:
while (*p != '\0') { cout << *p; p++; }
Every string.h function below is secretly this loop wearing a different uniform. Understand the walk once and all five functions become obvious.
Practical
The five workers of <cstring>
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char a[20] = "Masala";
char b[] = "Dosa";
cout << strlen(b) << endl; // 4, the guard \0 is not counted
strcat(a, b); // a becomes "MasalaDosa"
cout << a << endl;
char copy[20];
strcpy(copy, a); // copy now holds "MasalaDosa"
cout << strcmp("Chai", "Chai") << endl; // 0 means EQUAL
cout << strcmp("Chai", "Dosa") << endl; // negative: C before D
return 0;
}
At a glance
string.h at a glance
| Function | Job | Remember |
|---|---|---|
| strlen(s) | Count characters | Excludes the \0 |
| strcpy(d, s) | Copy s into d | d must be big enough |
| strcat(d, s) | Append s onto d | d needs room for both |
| strcmp(a, b) | Compare alphabetically | 0 = equal, sign = order |
| strrev(s) | Reverse in place | Non-standard (Turbo C) |
Think first
Count it yourself
char t[10] = "Chai";
Before tapping, answer both: what does strlen(t) return, and how many bytes of the array does "Chai" actually occupy?
Show the answer
strlen(t) returns 4: it counts visible characters and stops at the guard. But the text occupies 5 bytes: C, h, a, i, \0. This gap is the most-tested string fact there is. Declare an array of size n and it can hold at most a string of length n minus 1.
Quiz
The till checks membership: if (strcmp(entered, "STAFF123")). The password matches exactly. What does strcmp return, and does the if run?
- Returns 0, so the if does NOT run
- Returns 1 (true), so the if runs
- Returns 0, and 0 is true so the if runs
- It returns the string length, 8
Show the answer
Returns 0, so the if does NOT run
Equal strings make strcmp return 0, and in C++ zero is false, so the if block is skipped. This inverted logic catches thousands of students: the correct equality test is strcmp(a, b) == 0. Option B is the exact misconception being tested; strcmp never returns lengths, and 0 is never true.
Watch out
Marks leak here
strcmp 0 confusion: write in your answer that 0 means equal, negative means the first string comes earlier alphabetically, positive means later.
strcpy overflow: copying 15 characters into a char[10] compiles fine and corrupts memory at runtime.
strrev is not standard: it exists in Turbo C but not in GCC. In a GCC environment you reverse with your own loop; say so if asked.
Theory
The bridge ahead
C++ later offers the std::string class, which owns its memory, allows = and +, and removes most of these traps. Your exam still tests char arrays and string.h first, because they teach what text really is in memory. When you reach classes, notice that std::string is just a class somebody wrote around this very train-and-guard idea.
Summary
Key takeaways
- A C-string is a char array ending with the null terminator \0: "Chai" needs 5 bytes for 4 letters.
- A char pointer walks the array coach by coach until it meets \0.
- strlen counts without \0; strcpy copies; strcat appends; both need a big-enough destination.
- strcmp returns 0 for EQUAL (test with == 0), negative or positive for alphabetical order.
- strrev reverses in place but is non-standard (Turbo C only, not GCC).
- Arrays cannot be assigned with =; copying text is strcpy's job.
- Memory hook: the train with a guard coach.