(standard c libraries translation )index

index, rindex - locate character in string
index,rindex-在字符串中定位字符

所需头文件
#include <strings.h>
char *index(const char *s, int c);
char *rindex(const char *s, int c);

The index() function returns a pointer to the first occurrence of the character c in the string s.
The rindex() function returns a pointer to the last occurrence of the character c in the string s.
The terminating null byte ('\0') is considered to be a part of the strings.
index函数返回一个指向字符串s中字符c第一次出现的位置
rindex函数返回一个指向字符串s中字符c最后一次出现的位置
字符串必须包含结束字符\0

返回值
The index() and rindex() functions return a pointer to the matched character or NULL if the character is not found.
index()和rindex()函数返回一个指向匹配字符的指针,如果没有找到则返回NULL

marked  as  LEGACY  in  POSIX.1-2001.   POSIX.1-2008 removes the specifications of index() and rindex(), recommending strchr(3) and strrchr(3) instead.
在POSIX.1-2001中被标记,在POSIX.1-2008中删掉了index()和rindex的定义,推荐使用strchr和strrchr作为替代


testcase如下:

#include <stdio.h>
#include <string.h>

int main(void)
{
	char *s = "abcddcba";

	char *tmp = index(s, 'b');
	printf("tmp = %s\n", tmp);

	tmp = rindex(s, 'b');
	printf("tmp = %s\n", tmp);
	return 0;
}

运行结果如下:

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ ./a.out
tmp = bcddcba
tmp = ba

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值