#navi(../)
* 文字列同士を比較する・strcmp [#k9d8e081]
文字列と文字列が同じかどうか比較するにはstrcmpを使用します。~
以下にstrcmpを使用した例を記します。
#contents
#htmlinsertpcsp(c-top.html,c-sp.html)
* 関連記事 [#ke2e9cb0]
-[[文字列同士を比較する・strcmp>文字列/文字列同士を比較する・strcmp]]
-[[大文字小文字を区別せずに文字列同士を比較する・strcasecmp>文字列/大文字小文字を区別せずに文字列同士を比較する・strcasecmp]]
* strcmpの書式 [#f72ee471]
-必要なインクルードファイル
#include <string.h>
-書式
int strcmp(const char *s1, const char *s2);
書式を確認すると以下のようになります。~
-戻り値~
同じ場合は0が返却される。~
異なる場合は0以外が返却される。
-引数~
1つ目: 1つ目の文字列のポインタ~
2つ目: 2つ目の文字列のポインタ
* strcmpのCサンプルコード [#d71e8d0c]
&ref(strcmp.c); (改行コードLF)
#include <stdio.h>
#include <string.h>
int main(void)
{
char *a = "abcdefg";
char *b = "abcdefg";
char *c = "ABCDEFG";
if (0 == strcmp(a, b)) {
printf("strcmp(a, b) : a and b are the same.\n");
}
else {
printf("strcmp(a, b) : a and b are different.\n");
}
if (0 == strcmp(b, c)) {
printf("strcmp(b, c) : a and b are the same.\n");
}
else {
printf("strcmp(b, c) : a and b are different.\n");
}
return 0;
}
* strcmpのCサンプルコード実行結果 [#h0c01cfa]
以下にコンパイルおよび実行結果を記します。
$ gcc strcmp.c -o strcmp
$ ./strcmp
strcmp(a, b) : a and b are the same.
strcmp(b, c) : a and b are different.
以上、strcmpのCサンプルコードでした。
#htmlinsertpcsp(c-btm.html,c-sp.html)