C语言实现字符串中查找字符串

获取b字符串在a中第一次出现的位置的算法题

int stringFind(const char *string, const char *dest) {
    if (string == nil || dest == nil) return -1;

    int i = 0;
    int j = 0;
    while (string[i] != '\0') {
        if (string[i] != dest[0]) {
            i ++;
            continue;
        }
        j = 0;
        while (string[i+j] != '\0' && dest[j] != '\0') {
            if (string[i+j] != dest[j]) {
                break;
            }
            j ++;
        }
        if (dest[j] == '\0') return i;
        i ++;
    }
    return -1;
}
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"%d", StringFind("fflowerdddddd", "flower"));
        NSLog(@"%d", stringFind("fflowerdddddd", "flower"));
    }
    return 0;
}

你可能感兴趣的:(C语言实现字符串中查找字符串)