負 : 文字列 1 が文字列 2 より小さい 0 : 文字列 1 と文字列 2 は等しい 正 : 文字列 1 が文字列 2 より大きい
#include <string.h> int strncmp(const char *str1, const char *str2, size_t num) str1 : 文字列 1 str2 : 文字列 2 num : 比較する文字数
#include <stdio.h>
#include <string.h>
int main()
{
int comp1, comp2, comp3;
char *str1 = "abcde";
char *str2 = "abcx";
char *str3 = "ABcde";
comp1 = strcmp(str1, str2);
comp2 = strncmp(str1, str2, 3);
comp3 = strcmp(str1, str3);
printf("文字列 %s と文字列 %s は\n", str1, str2);
printf(" 全体としては %d\n", comp1);
printf(" 最初の3文字は %d\n", comp2);
printf("文字列 %s と文字列 %s は\n", str1, str3);
printf(" 全体として %d\n", comp3);
return 0;
}
文字列 abcde と文字列 abcx は 全体としては -20 最初の3文字は 0 文字列 abcde と文字列 ABcde は 全体として 32
| 菅沼ホーム | 本文目次 | 演習問題解答例 | 付録目次 | 索引 |