strrchr

  • Locate last occurrence of character in string, Returns a pointer to the last 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 to retrieve a pointer to the end of a string.
  • 寻找 ch (如同用 (char)ch 转换到 char 后)在 str 所指向的空终止字节串中(将每个字符转译成 unsigned char )的最后出现。若搜索 '\0' ,则认为终止空字符为字符串的一部分,而且能找到。
  • C 库函数 char *strrchr(const char *str, int c) 在参数 str 所指向的字符串中搜索最后一次出现字符 c(一个无符号字符)的位置。
  • 若 str 不是指向空终止字节串的指针,则行为未定义。
char *strrchr( const char *str, int ch );

Parameters

str

  • C string.
  • 指向要分析的空终止字节字符串的指针

character

  • Character to be located. It is passed as its int promotion, but it is internally converted back to char.
  • 要搜索的字符
  • 要搜索的字符。以 int 形式传递,但是最终会转换回 char 形式。

Return Value

  • A pointer to the last occurrence of character in str.If the character is not found, the function returns a null pointer.
  • 指向 str 中找到的字符的指针,或若找不到这种字符则为空指针。
  • 该函数返回 str 中最后一次出现字符 c 的位置。如果未找到该值,则函数返回一个空指针。

Example

//
// Created by zhangrongxiang on 2018/2/6 9:12
// File strrchr
// #include <stdio.h>
#include <string.h> //该函数返回 str 中最后一次出现字符 c 的位置。如果未找到该值,则函数返回一个空指针。
int main() {
const char str[] = "https://github.com/zhangrxiang/learn-c";
const char str2[] = "D:\\WorkSpace\\clionProjects\\learn-c\\string\\strrchr.c";
const char ch = '/';
const char ch2 = '\\';
char *ret; ret = strrchr(str, ch);
// |/| ------ |/learn-c|
printf("|%c| ------ |%s|\n", ch, ret);
printf("%c\n", ret[0]);// /
printf("%ld\n", ret - str + 1);//number 31
printf("%ld\n", ret - str);//index 30 ret = strrchr(str2, ch2);
// |\| ------ |\strrchr.c|
printf("|%c| ------ |%s|\n", ch2, ret);
printf("%c\n", ret[0]); /*** \ */
printf("%c\n", str2[ret - str2]);/*** \ */
printf("%ld\n", ret - str2 + 1);//42
printf("%d\n", (int) strlen(ret));//10
printf("%s\n", ret + 1);//strrchr.c
printf("%c\n", *ret); /*** \ */
printf("%c\n", *(ret + sizeof(char)));//s
printf("%c\n", *(ret + sizeof(char) * 2));//t
printf("%s\n", &(*ret));// \strrchr.c
printf("%s\n", &*(ret + sizeof(char)));//strrchr.c ret = strrchr(str,'A');
if (ret){
printf("exists A\n");
} else{
// no exists A
printf("no exists A\n");
}
return 0;
}

参考文章

转载注明出处

C 标准库 - string.h之strrchr使用的更多相关文章

  1. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

  2. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  3. C 标准库 - string.h之strpbrk使用

    strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...

  4. C标准库string.h中几个常用函数的使用详解

    strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...

  5. C 标准库 - string.h之memmove使用

    memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...

  6. C 标准库 - string.h之memcpy使用

    memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...

  7. C 标准库 - string.h之memcmp使用

    memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...

  8. C 标准库 - string.h之memchr使用

    memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...

  9. C 标准库 - string.h之strlen使用

    strlen Returns the length of the C string str. The length of a C string is determined by the termina ...

随机推荐

  1. robot中使用evaluate转化数据格式

    如果你使用robot却没有用过evaluate,那你将永远禁锢在框架中. json对象格式入参可以使用字典格式直接传入,但最近有一个接口测试的入参是一个json数组,在传参时总是提示请求参数不合法, ...

  2. Django Query

    Making Qeries 一旦创建了数据模型,Django就会自动为您提供一个数据库抽象API,允许您创建.检索.更新和删除对象.本文档解释了如何使用这个API. The models 一个clas ...

  3. Python【读取文件,第一行与最后一行】

    文件小的读取方法 with open("a1.txt","r",encoding="gbk") as f: r = f.readlines( ...

  4. [BJ2006] 狼抓兔子

    题目链接:戳我 按理说以dinic\(O(M*N^2)\)的时间复杂度应该是过不去的(呃我也知道这个上界很松).但是最小割确实可以水过去?? 但是本着写正解的精神,我还是学了学平面图和对偶图,跑最短路 ...

  5. random 随机生成字符串

    # import random# for x in range(10):# i = 0# l = []# while i < 10:# ret = chr(random.randint(33, ...

  6. day10学python socket用户交互+MD5加密

    socket用户交互+MD5加密 利用socket从client传输文件指令于server 再返还字节大小与内容 socketserver的使用(重要) 注意: ##client.recv(1024) ...

  7. [BZOJ2049] [SDOI2008] 洞穴勘测

    题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...

  8. linux修改用户名

      貌似只需要改红色的就行了,要保险就都改,比如阿里云服务器就要2个都改   ubuntu:修改 /etc/hostname               修改 /etc/hosts  比如:127.0 ...

  9. python 数据分析 文章集锦

    文本分析: re&jieba模块  使用 正则表达式 和 中文处理模块jieba 原文地址:https://www.cnblogs.com/minutesheep/p/10357209.htm ...

  10. python3的enumerate函数

    enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中.