未分類

C言語でchar文字列の長さを取得する (strlen)

投稿日:

strlen 関数を使って取得すればよい。
sizeof という関数もあるが、strlen と振る舞いが異なるので注意。

#include<stdio.h>
#include<string.h>

int main () {

char moji[100] = "good morning";

printf("strlen(moji)=%lu\n", strlen(moji));
printf("sizeof(moji)=%lu\n", sizeof(moji));

return 0;
}

結果

strlen(moji)=12
sizeof(moji)=100

-未分類

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

python3 で、文字列の最後が特定の文字で終わるか判定する方法

endswith を使う。 例 str1 = “こんにちは” result1 = str1.endswith(“は”) result2 = str1.endswith(“わ”) print(resul …

no image

tex で数式モード内に普通の文字列を入力する方法

\textrm を使う。 例 $$y = f (x) \hspace{1cm} \textrm{This is a function.}$$ 結果

no image

c言語で文字をつなげる方法

sprintf 関数を使って、result 変数に文字列 s1 と文字列 s2 を結合した文字列を出力することができる。 例 #include<stdio.h> int main(void) { …

no image

python3 で数字をゼロ埋めして表示する

string の、zfill を使う。 例 n = 3 n0 = str(n).zfill(10) print(n0) 結果 0000000003

no image

tex でファイ(φ、Φ)を入力する方法

小文字のファイ:φ、大文字のファイ:Φ串刺しでないファイ($\varphi$)も使われることがある。 例 $\phi, \varphi, \Phi$ 結果