【C】库函数之 strchr

目录

1. Locate first occurrence of character in string

2. 源代码

3. 输出结果


1. Locate first occurrence of character in string

#include <string.h>
char * strchr ( const char * str, int character );

Returns a pointer to the first occurrence of character in the C string str.

The terminating null-character is considered part of the C string. Therefore, it can also be located in order to retrieve a pointer to the end of a string.

上述内容是 cplusplus 对 strchr 函数的介绍,

可以看出 strchr 函数返回要查找字符第一次出现的位置,如果没有查找成功,则返回空指针

2. 源代码

#include <stdio.h>
#include <assert.h>

#define SRC_STR "hello"
#define FIND_CHAR 'l'
  
char *Strchr(const char *src, int c) {
    assert(NULL != src);
   
    while (('\0' != *src) && (*src != (char)c))
        ++src;
   
    if ((char)c == *src)
        return (char *)src;
   
    return NULL;
}  
   
void test() {
    char *ret = Strchr(SRC_STR, FIND_CHAR);
   
    if (NULL != ret)
        printf("call Strchr, find [%c/%s] in src: %s\n", FIND_CHAR, ret, SRC_STR);
    else
        printf("call Strchr, not find [%c/%s] in src: %s\n", FIND_CHAR, ret, SRC_STR);
}  
   
int main(void) {  
    test();
   
    return 0;
}

3. 输出结果

call Strchr, find [l/llo] in src: hello

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值