Strings: char arrays and the null terminator · String: array char dan terminator null
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.
Teks adalah array of chars
- C tidak memiliki tipe string terpisah. Sebuah string hanyalah array dari
char. char word[] = "hi";sebenarnya menyimpan'h','i', dan'\0'tersembunyi di akhir.- Anda dapat membaca satu karakter pada satu waktu dengan index, sama persis seperti array apa pun.
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.
Terminador tak terlihat '\0'
- Setiap string C diakhiri dengan null terminator khusus, ditulis
'\0'(byte yang bernilai0). - Ini menandai di mana teks berakhir. Kata
"hi"mengambil tiga byte:'h','i','\0'. - Fungsi tahu bahwa sebuah string telah berakhir ketika mereka mencapai
'\0'— tidak ada panjang yang disimpan.
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.
Menelusuri string
- Untuk mengunjungi setiap karakter, loopkan index selama
s[i] != '\0'. - Ketika
s[i]berubah menjadi'\0', Anda telah mencapai akhir — berhenti. - Inilah cara Anda menghitung karakter, mencari, atau menyalin string secara manual.
#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.
Kotak alat <string.h>
<string.h>memiliki alat bantu siap pakai:strlen(s)(panjang),strcpy(dst, src)(salin),strcmp(a, b)(bandingkan),strcat(dst, src)(gabungkan).strcmp(a, b)mengembalikan0ketika dua string sama — berguna dalam tes.- Semua ini bergantung pada terminator
'\0'untuk mengetahui di mana setiap string berakhir.
Common mistakes
- A string needs one extra byte for
\0: a 5-letter word needschar[6]. - Compare strings with
strcmp, not==.
Kesalahan umum
- Sebuah string membutuhkan satu byte ekstra untuk
\0: kata 5 huruf membutuhkanchar[6]. - Bandingkan string dengan
strcmp, bukan==.
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.
Sekarang Anda coba
- Untuk mengubah string di tempat, pemanggil harus memberikan array
charyang dapat ditulis, bukan teks tetap. - Hentikan loop Anda di
'\0'. Jangan tulismain— checker menyediakan satu.
Strings are arrays of characters · String adalah array karakter
Index each character from 0; the slice shows a range of chars. · Indekskan setiap karakter dari 0; irisan menunjukkan rentang karakter.
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. · Lengkapi int count_char(const char *s, char c) agar mengembalikan berapa kali c muncul dalam string s. Lakukan perulangan hingga mencapai akhir string. Jangan tulis main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Lengkapi int my_strlen(const char *s) agar mengembalikan panjang s tanpa memanggil strlen. Jalankan loop array hingga mencapai akhir. Jangan tulis main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Lengkapi void reverse_str(char *s) agar membalikkan urutan karakter s secara in-place. Temukan panjangnya, lalu tukar dari kedua ujung menuju tengah. Jangan tulis main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.