Strings: char arrays and the null terminator
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
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.
日本語
文字列はchar配列である
- Cには独立した文字列型はありません。文字列とは単なる
charの配列です。 char word[] = "hi";は実際には'h'、'i'、そして末尾に隠れた'\0'を格納しています。- インデックスを使って文字を1つずつ読み取ることができます。これはどの配列でも同じように動作します。
English
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文字列は特殊なnul終端文字で終わります。これを
'\0'と表記します(0と同じバイト値)。 - これがテキストの終わりを示します。文字
"hi"は3バイトを占有します:'h'、'i'、'\0'。 - 文字列の終端に
'\0'が到達したことを関数が検知するため、文字列には長さが保存されていません。
English
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'になったら終了 reached — 停止します。- これが手動で文字数を数えたり、検索したり、文字列をコピーする方法です。
#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;
}
English
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)は2つの文字列が等しい場合に0を返すので、テスト時に便利です。- これらはすべて、各文字列の終わりを
'\0'終端文字によって判別するために依存しています。
English
Common mistakes
- A string needs one extra byte for
\0: a 5-letter word needschar[6]. - Compare strings with
strcmp, not==.
日本語
よくあるミス
- 文字列には
\0のための余分な1バイトが必要です:5文字の単語ならchar[6]必要です。 - 文字列を比較するには
strcmpを使用し、==は使用しないでください。
English
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を入力しないでください。チェックツールが自動で提供します。
Explore · 探索
Strings are arrays of characters
Index each character from 0; the slice shows a range of chars.
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.
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.
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.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。