佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1325|回复: 2

求助C Programming 高手 這個問題

[复制链接]
发表于 18-6-2010 05:45 PM | 显示全部楼层 |阅读模式
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.
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 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;
}

这样可以吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 27-11-2025 12:30 AM , Processed in 0.131632 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表