|
查看: 1324|回复: 2
|
求助C Programming 高手 這個問題
[复制链接]
|
|
|
Can you write in in C function ?
Find position of first occurrence of a case-insensitive string
int stripos ( char* haystack, char* needle, int offset )
Returns the numeric position of the first occurrence of needle in the haystack string. Note that the needle may be a string of one or more characters. If needle is not found, stripos() will return -1.
The function should not make use of any C library function calls. |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 18-6-2010 08:30 PM
|
显示全部楼层
回复 1# whiteShadow
problem solved, 問題解決了. 關貼 |
|
|
|
|
|
|
|
|
|
|
发表于 8-7-2010 12:36 AM
|
显示全部楼层
int stripos ( char* haystack, char* needle, int offset )
{
int position = -1;
int positionCtr = -1;
int matchedLenght = 0;
int needleLength = 0;
char * tmp = haystack;
char * needleTmp = needle;
tmp = (tmp + offset);
while ( *needleTmp != '\0' && *needleTmp != '\n' ){
needleLength++; needleTmp++;
}
needleTmp = needle;
tmp = (tmp + offset);
positionCtr = offset;
while ( *tmp != '\0' && *tmp != '\n' ) {
if ( *tmp == *needleTmp )
{
if ( matchedLenght++ == 0 ){
position = positionCtr;
}
needleTmp++;
} else if ( matchedLenght > 0 ){
needleTmp = needle;
position = -1;
matchedLenght = 0;
}
positionCtr++;
tmp++;
}
return position;
}
这样可以吗? |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|