:: reverse dictionary ::
※ソースファイルについて
文字列を前から検索するには、strstr 関数を使います。
#include <string.h>
char str[64] = "WindowsサーバとLinuxサーバ"; char *p; p = strstr(str, "サーバ");
p は "サーバとLinuxサーバ" になります
ワイド文字列の場合は wcsstr 関数を使います。
#include <locale.h> #include <string.h>
wchar_t str[64] = L"WindowsサーバとLinuxサーバ"; wchar_t *p; setlocale(LC_ALL, "ja"); /* ロケールを日本語に設定 */ p = wcsstr(str, L"サーバ");
p は "サーバとLinuxサーバ" になります
char *strstr(const char *s, const char *sk) wchar_t *wcsstr(const wchar_t *s, const wchar_t *sk)
検索文字列 sk を対象の文字列 s の最初から検索します。
検索文字列 sk が見つかった場合、その先頭文字のポインタを返します。 検索文字列 sk が見つからなかった場合は NULL を返します。
char str[64] = "WindowsサーバとLinuxサーバ"; char *p; p = strstr(str, "Solarisサーバ");
p は NULL になります。
ワイド文字列も同じです。
wchar_t str[64] = L"WindowsサーバとLinuxサーバ"; wchar_t *p; setlocale(LC_ALL, "ja"); /* ロケールを日本語に設定 */ p = wcsstr(str, L"Solarisサーバ");
p は NULL になります。
s … 対象の文字列
sk … 検索文字列
・検索文字列 sk が見つかった … その先頭文字のポインタ
・検索文字列 sk が見つからなかった … NULL
見つかった文字列の先頭のインデックスは、計算で求めることができます。
char str[64] = "WindowsサーバとLinuxサーバ"; char *p; int index; p = strstr(str, "Linux"); index = p - str;
index は 15 になります。
( str[15] = 'L' )
ワイド文字列の場合も同じです。
wchar_t str[64] = L"WindowsサーバとLinuxサーバ"; wchar_t *p; int index; setlocale(LC_ALL, "ja"); /* ロケールを日本語に設定 */ p = wcsstr(str, L"Linux"); index = p - str;
index は 11 になります。
( str[11] = L'L' )
Copyright (C) 2005-2007 Noto Watabe. All rights reserved.
e-mail:wmh@always-pg.com