Strings: char arrays and the null terminator · 字符串:字符数组与结束符
Text is an array of chars
- C has no separate string type. A string is just an array of
char. char word[] = "hi";actually stores'h','i', and a hidden'\0'at the end.- You can read one character at a time with an index, exactly like any array.
文本就是字符数组
- C 没有单独的字符串类型。字符串就是一个
char数组。 char word[] = "hi";实际上存了'h'、'i',以及末尾一个隐藏的'\0'。- 你可以用下标一个字符一个字符地读,和任何数组完全一样。
The invisible terminator '\0'
- Every C string ends with a special null terminator, written
'\0'(a byte equal to0). - It marks where the text stops. The word
"hi"takes three bytes:'h','i','\0'. - Functions know a string has ended when they reach
'\0'— there is no stored length.
看不见的结束符 '\0'
- 每个 C 字符串都以一个特殊的空字符结束符结尾,写作
'\0'(一个等于0的字节)。 - 它标记文本在哪里停止。单词
"hi"占 三 个字节:'h'、'i'、'\0'。 - 函数遇到
'\0'就知道字符串结束了 —— 字符串并不存储自己的长度。
Walking a string
- To visit each character, loop an index while
s[i] != '\0'. - When
s[i]becomes'\0', you have reached the end — stop. - This is how you count characters, search, or copy a string by hand.
遍历字符串
- 要访问每个字符,让下标在
s[i] != '\0'时循环。 - 当
s[i]变成'\0'时,你就到了末尾 —— 停下来。 - 这就是手动统计字符、查找或复制字符串的方法。
#include <stdio.h>
int main(void) {
char word[] = "hello";
int len = 0;
while (word[len] != '\0') { // stop at the terminator
len++;
}
printf("%d\n", len); // 5
return 0;
}
The <string.h> toolbox
<string.h>has ready-made helpers:strlen(s)(length),strcpy(dst, src)(copy),strcmp(a, b)(compare),strcat(dst, src)(join).strcmp(a, b)returns0when the two strings are equal — handy in tests.- These all rely on the
'\0'terminator to know where each string ends.
<string.h> 工具箱
<string.h>提供了现成的助手:strlen(s)(长度)、strcpy(dst, src)(复制)、strcmp(a, b)(比较)、strcat(dst, src)(连接)。strcmp(a, b)在两个字符串相等时返回0—— 在测试里很好用。- 它们都依赖
'\0'结束符来知道每个字符串在哪里结束。
Common mistakes
- A string needs one extra byte for
\0: a 5-letter word needschar[6]. - Compare strings with
strcmp, not==.
常见错误
- 字符串要多留一个字节给
\0:5 个字母的词要char[6]。 - 比较字符串用
strcmp,不是==。
Now you try
- To change a string in place, the caller must pass a writable
chararray, not a fixed text. - Stop your loops at
'\0'. Do not write amain— the checker provides one.
现在轮到你了
- 要就地修改字符串,调用者必须传进来一个可写的
char数组,而不是固定的文本。 - 让你的循环在
'\0'处停下。不要自己写main—— 检查器会提供。
Strings are arrays of characters · 字符串是字符数组
Index each character from 0; the slice shows a range of chars. · 每个字符从 0 开始索引;切片显示一段字符。
Complete int count_char(const char *s, char c) so it returns how many times c appears in the string s. Loop until you reach the end of the string. Do not · 不 write a main. · 完成 int count_char(const char *s, char c),让它返回字符 c 在字符串 s 中出现的次数。循环到字符串结束为止。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete int my_strlen(const char *s) so it returns the length of s without calling strlen. Walk the array until you reach the end. Do not · 不 write a main. · 完成 int my_strlen(const char *s),让它返回 s 的长度,但不调用 strlen。遍历数组直到末尾。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete void reverse_str(char *s) so it reverses the characters of s in place. Find the length, then swap from both ends toward the middle. Do not · 不 write a main. · 完成 void reverse_str(char *s),让它就地反转 s 里的字符。先求出长度,再从两端向中间交换。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。