Last active
August 29, 2015 13:58
-
-
Save PanNyaa/10099480 to your computer and use it in GitHub Desktop.
strchrとstrstrのオフセット指定後方検索verな関数。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
char* strchrback(const char *,const char *,int); | |
//strchrの後方検索版。オフセットを指定してそこから_Strの先頭まで探します | |
char* strchrback(const char *_Str,const char *offset ,int _C){ | |
while(_Str != offset){ | |
if(*offset == _C){ | |
return (char*)offset; | |
}else{ | |
offset--; | |
} | |
} | |
return NULL; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
char* strstrback(const char *,const char *,const char *); | |
//strstrの後方検索版。オフセットを指定してそこから_Strの先頭まで探します | |
//strstrの文法を参考にしました : http://www1.cts.ne.jp/~clab/hsample/Point/Point14.html | |
char *strstrback(const char *_Str,const char* offset, const char *_SubStr){ | |
int n; | |
n = strlen(_SubStr); /* 文字列_SubStrの長さを求める */ | |
for(;; offset-- ) { | |
offset = strchrback(_Str, offset , _SubStr[0]); /* _SubStrの先頭の1文字を後方検索で探す */ | |
if (offset == _Str)return (NULL); /* _Strの先頭まで検索し終えていた場合NULLを返す */ | |
if(strncmp(offset, _SubStr, n) == 0){ /* 見つかったらn文字比較 */ | |
return (char *)offset; /* 一致したらポインタを返す */ | |
} | |
/* ここに来た場合一致していないのでポインタをデクリメントし次の一致文字列を探す*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment