「C言語/標準ライブラリ/string.h」の版間の差分

削除された内容 追加された内容
preタグをsourceタグに修正
Ef3 (トーク | 投稿記録)
DL化
タグ: 2017年版ソースエディター
1 行
== 概要 ==
文字列操作の関数を使用するためには、ヘッダー<code>string.h</code>をインクルードする<ref>『JISX3010:2003』p.233「7.21 文字列操作<string.h>」</ref>。
string.hというヘッダファイルを組み込む必要がある。
<ref>『JISX3010:2003』p.233「7.21 文字列操作<string.h>」</ref>
 
次に文字列操作の関数を表にまとめた。
{|class="wikitable"
|+ 文字列操作の関数と分類
!形式!!機能
|-
65 ⟶ 63行目:
 
== ロケール ==
C言語におけるロケール(''locale'')とは、ユーザーの言語、地域、およびユーザーがユーザーインターフェイスで表示したい特別なバリアント設定を定義する一連のパラメーターです。
ロケールとは、ソフトウェアに内蔵される、言語や国・地域ごとに異なる単位、記号、日付、通貨などの表記規則の集合である。
<ref>[http://e-words.jp/w/E383ADE382B1E383BCE383AB.html ロケールとは - 意味/解説/説明/定義 : IT用語辞典]</ref>
 
ロケールを設定するには<code>locale.hのsetlocale</code>で宣言される関数<code>setlocale</code>を用いる。
詳細は[[C言語/標準ライブラリ/文化圏固有操作|文化圏固有操作<locale.h>]]を参照せよ。
 
== 型 ==
*; size_t
: size_tは、sizeof演算子の結果の符号なし整数型とする。<ref name="共通の定義">『JISX3010:2003』p.183「7.17 共通の定義<stddef.h>」; {{cite book | url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf | title= C11: WG14/N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x | at=p.288, §7.19 ''Common definitions'' | publisher=ISO/IEC}}</ref>
 
== マクロ ==
*; NULL
: NULLは、処理系定義の空ポインタ定数に展開する。<ref name="共通の定義"/>
 
== コピー関数 ==
<ref>『JISX3010:2003』p.233「7.21.2 コピー関数」</ref>
=== memcpy関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
</syntaxhighlight>
</source>
*; 引数
:s1 コピー先のオブジェクト
:s2 コピー元のオブジェクト
:n コピーする文字数
*; 返却値
:s1の値
*; 機能
:s2が指すオブジェクトから、s1が指すオブジェクトに、n文字コピーする。<br>s1とs2が重なっている場合、その動作は'''未定義'''である。<br>
: 返却値はs1の値を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
102 ⟶ 100行目:
int main(void)
{
char s1[3] = {'a', 'b', 'c'};
int i;
char s1s2[3] = {'ad', 'be', 'cf'};
char s2[3]={'d', 'e', 'f'};
 
printf("\nコピー前のs1の値;");
for (int i = 0; i < 3; ++ii++)
printf("%2x ", s1[i]);
printf("\nコピー前のs2の値:");
for (int i = 0; i < 3; ++ii++)
printf("%2x ", s2[i]);
 
116 ⟶ 113行目:
 
printf("\nコピー後のs1の値;");
for (int i = 0; i < 3; ++ii++)
printf("%2x ", s1[i]);
printf("\nコピー後のs2の値:");
for (int i = 0; i < 3; ++ii++)
printf("%2x ", s2[i]);
}
</syntaxhighlight>
</source>
=== memmove関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
void *memmove(void *s1, const void *s2, size_t n);
</syntaxhighlight>
</source>
*; 引数
:s1 コピー先のオブジェクト
:s2 コピー元のオブジェクト
:n コピーする文字数
*; 返却値
:s1の値
*; 機能
:s2が指すオブジェクトから、s1が指すオブジェクトに、n文字コピーする。<br>s1とs2が重なっていてもよい。<br>返却値はs1の値を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
145 ⟶ 142行目:
{
int i;
char s[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
 
printf("\nコピー前のsの値;");
for (i = 0; i < 6; i++i)
printf("%2x ", s[i]);
 
154 ⟶ 151行目:
 
printf("\nコピー後のsの値;");
for (i = 0; i < 6; i++i)
printf("%2x ", s[i]);
}
</syntaxhighlight>
</source>
 
=== strcpy関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
char *strcpy(char * restrict s1, const char * restrict s2);
</syntaxhighlight>
</source>
*; 引数
:s1 コピー先の文字
:s2 コピー元の文字列
*; 返却値
:s1の値
*; 機能
:s2が指す文字列(終端ナル文字を含む)をs1が指す配列にコピーする。<br>
:s1とs2が重なっている場合、その動作は'''未定義'''である。<br>
:返却値はs1の値を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
178 行
int main(void)
{
char s1[16] = "abc";
char s2[16] = "def";
printf("コピー前のs1の値:%s\n", s1);
189 行
printf("コピー後のs2の値:%s\n", s2);
}
</syntaxhighlight>
</source>
 
=== strncpy関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
char *strncpy(char * restrict s1, const char * restrict s2, size_t n);
</syntaxhighlight>
</source>
*; 引数
:s1 コピー先の文字
:s2 コピー元の文字
:n コピーする文字数
*; 返却値
:s1の値
*; 機能
:s2が指す配列から、s1が指す配列に、n個以下の文字(ナル文字に続く文字はコピーしない。)をコピーする。<br>
:s1とs2が重なっている場合、その動作は'''未定義'''である。<br>
:s2が指す配列がn文字より短い配列である場合、s1が指す配列中のコピーの後ろに、全体でn文字書き込むまでナル文字を付加する。<br>
:返却値はs1の値を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
211 ⟶ 215行目:
int main(void)
{
char s1[16] = "abc";
char s2[16] = "def";
printf("コピー前のs1の値:%s\n", s1);
222 ⟶ 226行目:
printf("コピー後のs2の値:%s\n", s2);
}
</syntaxhighlight>
</source>
== 連結関数 ==
<ref>『JISX3010:2003』p.235「7.21.3 連結関数」</ref>
=== strcat関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
char *strcat(char * restrict s1, const char * restrict s2);
</syntaxhighlight>
</source>
*; 引数
:s1 連結先の文字列
:s2 連結する文字列
*; 返却値
:s1の値
*; 機能
:s2が指す文字列(終端ナル文字を含む。)のコピーをs1が指す文字列の最後に付加する。<br>s2の先頭の文字が、s1が指す文字列の最後のナル文字を書き換える。<br>s1とs2が重なっている場合、その動作は'''未定義'''である。<br>
: 返却値はs1の値を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
245 ⟶ 250行目:
int main(void)
{
char s1[16] = "abc";
char s2[16] = "def";
printf("付加前のs1の値:%s\n", s1);
256 ⟶ 261行目:
printf("付加後のs2の値:%s\n", s2);
}
</syntaxhighlight>
</source>
=== strncat関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
char *strncat(char * restrict s1, const char * restrict s2, size_t n);
</syntaxhighlight>
</source>
*; 引数
:s1 連結先の文字列
:s2 連結する配列
:n 連結する文字数
*; 返却値
:s1の値
*; 機能
:s2が指す配列からn個以下の文字を、s1が指す文字列の最後に付加する。<br>(ナル文字及びナル文字に続く文字は付加しない。)<br>s2の先頭の文字が、s1が指す文字列の最後のナル文字を書き換える。<br>終端ナル文字を常に結果に付加する。<br>s1とs2が重なっている場合、その動作は'''未定義'''である。<br>
: 返却値はs1の値を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
278 ⟶ 284行目:
int main(void)
{
char s1[16] = "abc";
char s2[16] = "def";
printf("付加前のs1の値:%s\n", s1);
289 ⟶ 295行目:
printf("付加後のs2の値:%s\n", s2);
}
</syntaxhighlight>
</source>
== 比較関数 ==
<ref>『JISX3010:2003』p.236「7.21.4 比較関数」</ref>
298 ⟶ 304行目:
 
=== memcmp関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
int memcmp(const void *s1, const void *s2, size_t n);
</syntaxhighlight>
</source>
*; 引数
:s1 比較するオブジェクト
:s2 比較するオブジェクト
:n 比較する文字数
*; 返却値
:比較の結果
*; 機能
:s1が指すオブジェクトの始めのn文字と、s2が指すオブジェクトの始めのn文字を比較する。<br>
: 返却値はs1がs2より大きいか、等しいか、又は小さいかによって、それぞれ、0より大きい、0に等しい、又は0より小さい整数を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
319 ⟶ 326行目:
{
int ret;
char s1[] = {'a', 'b', 'c'};
char s2[] = {'a', 'd', 'e'};
ret=memcmp(s1, s2, 1);
if (ret==0)
printf("s1とs2の初めの1文字を比較すると、s1はs2と等しい。\n");
else if (ret>0)
printf("s1とs2の初めの1文字を比較すると、s1はs2より大きい。\n");
else if (ret<0)
printf("s1とs2の初めの1文字を比較すると、s1はs2より小さい。\n");
 
ret=memcmp(s1, s2, 2);
if (ret==0)
printf("s1とs2の初めの2文字を比較すると、s1はs2と等しい。\n");
else if (ret>0)
printf("s1とs2の初めの2文字を比較すると、s1はs2より大きい。\n");
else if (ret<0)
printf("s1とs2の初めの2文字を比較すると、s1はs2より小さい。\n");
}
</syntaxhighlight>
</source>
=== strcmp関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
int strcmp(const char *s1, const char *s2);
</syntaxhighlight>
</source>
*; 引数
:s1 比較する文字列
:s2 比較する文字列
*; 返却値
:比較の結果
*; 機能
:s1が指す文字列とs2が指す文字列を比較する。<br>
:返却値はs1が指す文字列が、s2が指す文字列より大きいか、等しいか、又は小さいかによって、それぞれ、0より大きい、0に等しい、又は0より小さい整数を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
360 ⟶ 368行目:
{
int ret;
char s1[] = "abc";
char s2[] = "ade";
ret=strcmp(s1, s2);
if (ret==0)
printf("s1とs2を比較すると、s1はs2と等しい。\n");
else if (ret>0)
printf("s1とs2を比較すると、s1はs2より大きい。\n");
else if (ret<0)
printf("s1とs2を比較すると、s1はs2より小さい。\n");
}
</syntaxhighlight>
</source>
=== strcoll関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
int strcoll(const char *s1, const char *s2);
</syntaxhighlight>
</source>
*; 引数
:s1 比較する文字列
:s2 比較する文字列
*; 返却値
:比較の結果
*; 機能
:s1が指す文字列とs2が指す文字列を比較する。<br>このとき、いずれの文字列も、その時点のロケールのLC_COLLATEカテゴリに従って解釈する。<br>返却値は、s1が指す文字列及びs2が指す文字列をその時点のロケールに従って解釈したとき、<br>s1が指す文字列が、s2が指す文字列より大きいか、等しいか、又は小さいかによって、それぞれ、0より大きい、0に等しい、又は0より小さい整数を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <locale.h>
394 ⟶ 402行目:
{
int ret;
char s1[] = "abc";
char s2[] = "ABC";
 
setlocale(LC_COLLATE, "C");
ret=strcoll(s1, s2);
if (ret==0)
printf("s1とs2を\"C\"ロケールで比較すると、s1はs2と等しい。\n");
else if (ret>0)
printf("s1とs2を\"C\"ロケールで比較すると、s1はs2より大きい。\n");
else if (ret<0)
printf("s1とs2を\"C\"ロケールで比較すると、s1はs2より小さい。\n");
 
setlocale(LC_COLLATE, "JPN");
ret=strcoll(s1, s2);
if (ret==0)
printf("s1とs2を\"JPN\"ロケールで比較すると、s1はs2と等しい。\n");
else if (ret>0)
printf("s1とs2を\"JPN\"ロケールで比較すると、s1はs2より大きい。\n");
else if (ret<0)
printf("s1とs2を\"JPN\"ロケールで比較すると、s1はs2より小さい。\n");
}
</syntaxhighlight>
</source>
=== strncmp関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
int strncmp(const char *s1, const char *s2, size_t n);
</syntaxhighlight>
</source>
*; 引数
:s1 比較する配列
:s2 比較する配列
:n 比較する文字数
*; 返却値
:比較の結果
*; 機能
:s1が指す配列をs2が指す配列と比較する。<br>比較する文字はn文字以下とする。(ナル文字に続く文字は比較しない)<br>返却値は、s1が指す配列(ナル文字で終了しうる)が、s2が指す配列(ナル文字で終了しうる)より大きいか、等しいか、又は小さいかによって、それぞれ、0より大きい、0に等しい、又は0より小さい整数を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
438 ⟶ 446行目:
{
int ret;
char s1[] = "abc";
char s2[] = "ade";
 
ret=strncmp(s1, s2, 1);
if (ret==0)
printf("s1とs2の初めの1文字を比較すると、s1はs2と等しい。\n");
else if (ret>0)
printf("s1とs2の初めの1文字を比較すると、s1はs2より大きい。\n");
else if (ret<0)
printf("s1とs2の初めの1文字を比較すると、s1はs2より小さい。\n");
 
ret=strncmp(s1, s2, 4);
if (ret==0)
printf("s1とs2の初めの4文字を比較すると、s1はs2と等しい。\n");
else if (ret>0)
printf("s1とs2の初めの4文字を比較すると、s1はs2より大きい。\n");
else if (ret<0)
printf("s1とs2の初めの4文字を比較すると、s1はs2より小さい。\n");
}
</syntaxhighlight>
</source>
=== strxfrm関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
size_t strxfrm(char * restrict s1, const char * restrict s2, size_t n);
</syntaxhighlight>
</source>
*; 引数
:s1 変換の結果の格納先の配列
:s2 変換する文字列
:n 格納する変換した結果の文字数
*; 返却値
:変換した結果の文字列の長さ
*; 機能
:s2が指す文字列を変換し、その結果の文字列をs1が指す配列に格納する。<br>
:その変換は次の通りとする。<br>
: すなわち、strcmp関数を変換結果の2つの文字列に適用した場合、strcmp関数が0より大きい値を返すか、0を返すか、又は0より小さい値を返すかは、2つの変換前の文字列をstrcoll関数に適用した結果と一致する。<br>
: 終端ナル文字を含めて、n個を超える文字を、s1が指す結果の配列に格納することはない。<br>
: nが0である場合、s1は空ポインタであってもよい。<br>
: s1とs2が重なっている場合、その動作は'''未定義'''である。<br>
: 返却値は、変換した結果の文字列(終端ナル文字は含まない)の長さを返す。<br>
: 返却された値がn以上の場合、s1が指す配列の内容は不定とする。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <locale.h>
482 ⟶ 497行目:
char *p;
char s1[16];
char s2[] = "abc";
 
setlocale(LC_COLLATE, "JPN");
488 ⟶ 503行目:
 
printf("\ns1の値;");
for (p = s1; *p != '\0'; p++p)
printf("%2x ", *p);
}
</syntaxhighlight>
</source>
 
== 探索関数 ==
<ref>『JISX3010:2003』p.236「7.21.5 探索関数」</ref>
=== memchr関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
void *memchr(const void *s, int c, size_t n);
</syntaxhighlight>
</source>
*; 引数
:s 探索の対象のオブジェクト
:c 探索する文字
:n 探索する文字数
*; 返却値
:捜し出した文字へのポインタ
*; 機能
:sが指すオブジェクトの先頭のn文字(各々unsigned char型として解釈する。)の中でc(unsigned char型に型変換する。)が最初に現れる位置を捜す。<br>返却値は、捜し出した文字へのポインタを返す。<br>オブジェクトの中にその文字が現れない場合、空ポインタを返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
515 ⟶ 531行目:
int main(void)
{
char s[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
char *p = memchr(s, 'd', 3);
 
if (p)
p=memchr(s, 'd', 3);
if(p)
printf("sの初めの3文字の中で'd'が最初に現れる位置は[%d]。\n", p-s);
else
printf("sの初めの3文字の中で'd'は現れない。\n");
 
p = memchr(s, 'd', 6);
if (p)
printf("sの初めの6文字の中で'd'が最初に現れる位置は[%d]。\n", p-s);
else
printf("sの初めの6文字の中で'd'は現れない。\n");
}
</syntaxhighlight>
</source>
=== strchr関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
char *strchr(const char *s, int c);
</syntaxhighlight>
</source>
*; 引数
:s 探索の対象の文字列
:c 探索する文字
*; 返却値
:捜し出した文字へのポインタ
*; 機能
:sが指す文字列の中で、c(char型に型変換する。)が最初に現れる位置を捜す。<br>終端ナル文字は文字列の一部とみなす。<br>返却値は、捜し出した文字へのポインタを返す。<br>文字列の中にその文字が現れない場合、空ポインタを返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
551 ⟶ 566行目:
int main(void)
{
char s[] = "abcdef";
char *p = strchr(s, 'd');
if (p)
p=strchr(s, 'd');
if(p)
printf("sの中で'd'が最初に現れる位置は[%d]。\n", p-s);
else
printf("sの中で'd'は現れない。\n");
}
</syntaxhighlight>
</source>
=== strcspn関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
size_t strcspn(const char *s1, const char *s2);
</syntaxhighlight>
</source>
*; 引数
:s1 探索の対象の文字列
:s2 探索する文字群
*; 返却値
:先頭部分の長さ
*; 機能
:s1が指す文字列の中で、s2が指す文字列中の文字を含まない先頭部分の最大の長さを計算する。<br>返却値は、その先頭部分の長さを返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
581 ⟶ 594行目:
int main(void)
{
char s1[] = "abcdef";
char s2[] = "abc";
char s3[] = "def";
int len;
592 ⟶ 605行目:
printf("%sの中で%sの文字群が含まれない先頭部分の最大の長さは%dです。\n", s1, s3, len);
}
</syntaxhighlight>
</source>
=== strpbrk関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
char *strpbrk(const char *s1, const char *s2);
</syntaxhighlight>
</source>
*; 引数
:s1 探索の対象の文字列
:s2 探索する文字群
*; 返却値
:文字へのポインタ
*; 機能
:s1が指す文字列の中で、s2が指す文字列の中のいずれかの文字が最初に現れる位置を捜す。<br>返却値は、その文字へのポインタを返す。<br>s1が指す文字列の中に、s2が指す文字列の中のいずれの文字も現れない場合、空ポインタを返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
613 ⟶ 626行目:
int main(void)
{
char s1[] = "abcdef";
char s2[] = "abc";
char s3[] = "def";
char *p = strpbrk(s1, s2);
 
if (p)
p=strpbrk(s1, s2);
if(p)
printf("%sの中で%sの文字群が最初に現れる位置は[%d]です。\n", s1, s2, p-s1);
else
printf("%sの中で%sの文字群は現れない。\n", s1, s2);
 
p = strpbrk(s1, s3);
if (p)
printf("%sの中で%sの文字群が最初に現れる位置は[%d]です。\n", s1, s3, p-s1);
else
printf("%sの中で%sの文字群は現れない。\n", s1, s3);
}
</syntaxhighlight>
</source>
=== strrchr関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
char *strrchr(const char *s, int c);
</syntaxhighlight>
</source>
*; 引数
:s 探索の対象の文字列
:c 探索する文字
*; 返却値
:文字へのポインタ
*; 機能
:sが指す文字列の中で、c(char型に型変換する。)が最後に現れる位置を捜す。<br>終端ナル文字は文字列の一部とみなす。<br>返却値はその文字へのポインタを返す。<br>文字列の中にcが現れない場合、空ポインタを返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
651 ⟶ 663行目:
int main(void)
{
char s[] = "abcdefabcdef";
char *p = strrchr(s, 'd');
 
if (p)
p=strrchr(s, 'd');
if(p)
printf("%sの中で'd'が最後に現れる位置は[%d]です。\n", s, p-s);
else
printf("%sの中で'd'は現れない。\n", s);
}
</syntaxhighlight>
</source>
=== strspn関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
size_t strspn(const char *s1, const char *s2);
</syntaxhighlight>
</source>
*; 引数
:s1 探索の対象の文字列
:s2 探索する文字群
*; 返却値
:先頭部分の長さ
*; 機能
:s1が指す文字列の中で、s2が指す文字列中の文字だけを含む先頭部分の最大の長さを計算する。<br>返却値は、その先頭部分の長さを返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
681 ⟶ 692行目:
int main(void)
{
char s1[] = "abcdef";
char s2[] = "abc";
char s3[] = "def";
int len;
692 ⟶ 703行目:
printf("%sの中で%sの文字群が含まれる先頭部分の最大の長さは%dです。\n", s1, s3, len);
}
</syntaxhighlight>
</source>
=== strstr関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
char *strstr(const char *s1, const char *s2);
</syntaxhighlight>
</source>
*; 引数
:s1 探索の対象の文字列
:s2 探索する文字列
*; 返却値
:探し出した文字列へのポインタ
*; 機能
:s1が指す文字列の中で、s2が指す文字列の中の文字の並び(終端ナル文字を除く。)が最初に現れる位置を捜す。<br>返却値は、探し出した文字列へのポインタを返す。<br>その文字列が見つからなかった場合、空ポインタを返す。<br>s2が長さ0の文字列を指す場合、s1を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
713 ⟶ 724行目:
int main(void)
{
char s1[] = "abcdef";
char s2[] = "abc";
char s3[] = "def";
char *p = strstr(s1, s2);
p=strstr(s1, s2);
printf("%sの中で%sの文字列が最初に現れる位置は[%d]です。\n", s1, s2, p-s1);
 
p = strstr(s1, s3);
printf("%sの中で%sの文字列が最初に現れる位置は[%d]です。\n", s1, s3, p-s1);
}
</syntaxhighlight>
</source>
 
=== strtok関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
char *strtok(char * restrict s1, char * restrict s2);
</syntaxhighlight>
</source>
*; 引数
:s1 分割の対象の文字列
:s2 字句を区切る文字群
*; 返却値
:字句の最初の文字へのポインタ
*; 機能
:strtok関数の一連の呼び出しは、s1が指す文字列を、s2が指す文字列の中のいずれかの文字で区切られる字句の列に分割する。<br>一連の呼び出しの最初の呼び出しでは、第1実引数として空ポインタ以外を指定する。<br>それ以降の呼び出しでは、第1実引数として空ポインタを指定する。<br>s2が指す区切り文字は、呼出しごとに異なっていてもよい。<br><br>一連の呼び出しの中の最初の呼び出しでは、s2が指すその時点での区切り文字列に含まれない最初の文字を、s1が指す文字列中で捜す。<br>その文字が見つからない場合、s1が指す文字列には字句が存在せず、strtok関数は空ポインタを返す。<br>その文字が見つかった場合、それを最初に字句の始まりとする。<br><br>次に、strtok関数は、見つかった文字の位置からその時点での区切り文字列に含まれている文字を捜す。その文字が見つからない場合、その時点の字句をs1が指す文字列の最後までとみなし、次回以降の探索では空ポインタを返す。<br>その文字が見つかった場合、その文字をナル文字で書き換え、その時点の字句を終了させる。<br>strtok関数はその次の文字へのポインタを保持しておき、字句の次の探索はそこから開始する。<br><br>第1実引数の値が空ポインタを持つ2回目以降の各呼び出しでは、保持したポインタが指すところから探索を開始する以外は、これと同じ動作をする。<br><br>処理系は、いかなるライブラリ関数もstrtok関数を呼び出さない場合の動作と同じ動作をしなければならない。<br><br>返却値は、字句の最初の文字へのポインタを返す。<br>字句が存在しない場合、空ポインタを返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
745 ⟶ 755行目:
int main(void)
{
char str[] = "abc def,ghi.";
char *p = strtok(str, " ,.");
 
p=strtok(str, " ,.");
printf("%s\n", p);
while (p != NULL) {
p = strtok(NULL, " ,.");
printf("%s\n", p);
}
}
</syntaxhighlight>
</source>
== その他の関数 ==
<ref>『JISX3010:2003』p.239「7.21.6 その他の関数」</ref>
=== memset関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
void *memset(void *s, int c, size_t n);
</syntaxhighlight>
</source>
*; 引数
:s セット先のオブジェクト
:c セットする文字
:n セットする文字数
*; 返却値
:
*; 機能
:c(unsigned char型に型変換する。)の値を、sが指すオブジェクトの最初のn文字のそれぞれにコピーする。<br>返却値は、sの値を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
784 ⟶ 792行目:
 
printf("\nsの値;");
for (i = 0; i < 6; i++i)
printf("%2x ", s[i]);
}
</syntaxhighlight>
</source>
=== strerror関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
char *strerror(int errnum);
</syntaxhighlight>
</source>
*; 引数
:errnum エラー番号
*; 返却値
:文字列へのポインタ
*; 機能
:errnumに入っている番号をメッセージ文字列に対応付ける。<br>典型的にはerrnumの値は、errnoが使われるが、strerror関数はint型の全ての値に対してメッセージを割り当てなければならない。<br><br>処理系は、いかなるライブラリ関数もstrerror関数を呼び出さない場合の動作と同じ動作をしなければならない。<br><br>返却値は、文字列へのポインタを返す。<br>その内容は、文化圏固有とする。<br>このポインタが指す配列をプログラムで変更してはならないが、strerror関数のそれ以降の呼び出しで書き変わることがある。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
809 ⟶ 817行目:
int errnum;
for (errnum=0; errnum<50; ++errnum)
printf("%2d:%s\n", errnum, strerror(errnum));
}
</syntaxhighlight>
</source>
; See also
: [[C言語/標準ライブラリ/入出力#perror関数]]
 
=== strlen関数 ===
*; 形式
: <sourcesyntaxhighlight lang=c>
#include <string.h>
size_t strlen(const char *s);
</syntaxhighlight>
</source>
*; 引数
:s 長さを計算する文字列
*; 返却値
:文字列の長さ
*; 機能
:sが指す文字列の長さを計算する。<br>返却値は、終端ナル文字に先行する文字の個数を返す。
*;
: <sourcesyntaxhighlight lang=c>
#include <stdio.h>
#include <string.h>
832 ⟶ 843行目:
int main(void)
{
char s[] = "abcdef";
int len;
len=strlen(s);
printf("%sの長さは%dです。\n", s, len);
}
</syntaxhighlight>
</source>
 
== 脚注 ==
843 ⟶ 854行目:
 
== 参考文献 ==
*; 日本工業標準調査会『JISX3010 プログラム言語C』2003年12月20日改正
 
[[Category:C言語|ひようしゆんらいふらり ]]