未分類

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

tex で文字列を変数として設定する方法

\newcommand を使って指定する。入力の数は自由に指定できる。 例 \newcommand{\variable}[2]{Hello #1. Good #2.} \variable{Taro}{ …

no image

tex で脚注を表示する方法

\footnote を使う。 例 \footnote{this is a footnote.} 結果

no image

python3 で、文字列のパーセント(%)をそのまま表示する方法

文字列の始まりに r をつけて、raw 文字列にするとパーセントなどの文字をそのまま表示できる。 例 str1 = r’%s%d\t’ print(str1) 結果 %s%d\t

no image

tex で同値記号(⇔)を入力する方法

同値記号を入力するには、\Leftrightarrow を使う。 例 $b \leftrightarrow c$, $b \Leftrightarrow c$

no image

c++ で文字列を「区切り文字」を使って分ける方法

string の find を使って、区切り文字の場所を取得し、その位置で区切る。 例 #include <iostream> #include <string> using namesp …