Strings: char arrays and the null terminator · מחרוזות: מערכות character והסיום 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.
טקסט הוא מערך של מקודדי ASCII
- ל-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 · מחרוזות הן מערכות של character
Index each character from 0; the slice shows a range of chars. · מספר את כל character באינדקס מ-0; החתך (slice) מציג טווח של character.
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) כדי להפוך את סדר הcharacter של הs במקור. מצא את האורך, ולאחר מכן החלף משני הקצוות לכיוון האמצע. אל תכתוב פקודת main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.