Stage 6: C-Style String Handling (char[])
A guide to low-level string processing as null-terminated character arrays, following exam constraints.
C-Style Strings: Char Arrays
Unlike the std::string class, C-style strings are simply arrays of elements of type char. The key element that distinguishes a regular character array from a "string" is the null terminator, written as \0. It tells functions like cout or printf where the text ends.
1. Declaration and Initialization
Remember that the array must always be 1 element larger than the number of characters you want to store to accommodate the \0.
char str[6] = "Hello"; // 5 characters + \0
char noSize[] = "Auto size"; // The compiler calculates the size automatically2. Input and Output
According to your exam rules, you can use both iostream and cstdio.
Using iostream:
std::cin >> str;– Reads text up to the first whitespace. Risky as it may exceed the array size.std::cin.getline(str, size);– The safest method. Reads a whole line (including spaces) and ensures the buffer is not overflowed.
Using cstdio:
scanf("%s", str);– Similar tocin, stops at whitespace.printf("%s\n", str);– Fast way to print text.
3. Manual String Operations (Without <cstring>)
Since your list of allowed libraries does not include <cstring> (which contains strlen or strcpy), you must be able to perform these operations manually using loops. This is a common test of skill in exams.
Calculating Length (Custom strlen)
int getLength(char tab[]) {
int i = 0;
while (tab[i] != '\0') {
i++;
}
return i;
}Copying a String (Custom strcpy)
void copyString(char dest[], char source[]) {
int i = 0;
while (source[i] != '\0') {
dest[i] = source[i];
i++;
}
dest[i] = '\0'; // Very important: adding the null terminator at the end!
}4. Pitfalls and Critical Errors
- Missing
\0: If you fill the array manually and forget the null terminator,coutwill print characters until it happens to hit a zero in memory, resulting in "garbage" on the screen or a crash. - Buffer Overflow: Attempting to read 20 characters into a
char[10]array. Always usecin.getlinewith the size specified. - Comparing Strings:
if (str1 == str2)will not work correctly forchar[]arrays (it compares memory addresses, not content). You must write your own function to compare them character by character.
Example: Comparing Strings
bool areEqual(char t1[], char t2[]) {
int i = 0;
while (t1[i] != '\0' && t2[i] != '\0') {
if (t1[i] != t2[i]) return false;
i++;
}
return t1[i] == t2[i]; // Check if both end at the same position
}You might also like
Stage 11: Pointer Arithmetic in C++
Understanding how C++ operates on memory addresses. Learn why ptr++ is more than just adding one.
Stage 10: Pointers, Address-of, and Dereference Operators
Understanding the foundations of memory addressing in C++: how pointers and low-level operators work.
Stage 4: Functions and Argument Passing in C++
An in-depth analysis of function definitions, prototypes, and the differences between passing by value and by reference.