Strings: char arrays and the null terminator · النصوص: مصفوفات الأحرف والمُ终结 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. · اضغط تشغيل لرؤية المخرجات هنا.