C语言字符篇(四)字符串查找函数
| #include <string.h> | |
|
char *strchr(const char *s, int c);
|
The strchr() function returns a pointer to the first occurrence of the character c in the string s.
|
| char *strrchr(const char *s, int c); | The strrchr() function returns a pointer to the last occurrence of the character c in the string s. |
| size_t strspn(const char *s, const char *accept); | The strspn() function calculates the length (in bytes) of the initial segment of s which consists entirely of bytes in accept. |
| size_t strcspn(const char *s, const char *reject); | The strcspn() function calculates the length of the initial segment of s which consists entirely of bytes not in reject. |
| char *strpbrk(const char *s, const char *accept); | The strpbrk() function returns a pointer to the byte in s that matches one of the bytes in accept, or NULL if no such byte is found. |
| char *strstr(const char *haystack, const char *needle); | These functions return a pointer to the beginning of the located substring, or NULL if the substring is not found. |
int main(int argc, char **argv) {
const char *buf="hello strchr";
char *p1;
char *p2;
p1=strchr(buf,'l'); //记录字符l第一次出现的位置,并范围第一次出现该字符的指针
printf("%s\n",p1); //llo strchr
p2=strrchr(buf,'l');//记录字符最后一次出现的位置,并范围第一次出现该字符的指针
printf("%s\n",p2); //lo strchr
}
int main(int argc, char **argv) {
const char *buf="hello world";
int len;
/*在buf中寻找buf2中的任意字符,也就是说,在buf中,如果碰到buf2中的任意字符,就结束寻找,并记录之前不相等部分的字符数*/
/*比如,在buf中有个空格是buf2可以匹配到的,在空格之前有5个字节是不匹配的,所以返回值为5*/
/*统计不同的数,直到出现了buf2中存在的字符*/
len=strcspn(buf,"\t\n,.?! "); //
printf("scpn:%d\n",len);
/*统计相同的数,直到出现了buf2里面不存在的字符*/
len=strspn(buf,"abcdefghijklmn"); //hell 都在buf2中出现过,所以开始统计,到 o,buf2中没有,返回到停止之前统计的字符数
printf("spn:%d\n",len);
}
int main(int argc, char **argv) {
const char *buf="hello,kmist";
char *p;
p = strpbrk(buf,"abcdefg");
printf("%s\n",p); //ello,kmist
}
int main(int argc, char **argv) {
const char *buf="hello,kmist";
char *p;
p = strstr(buf,"kmi");
printf("%s\n",p); //kmist ,如果没有就返回null
}
C语言字符篇(四)字符串查找函数的更多相关文章
- C/C++字符串查找函数
C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...
- C/C++字符串查找函数 <转>
C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...
- php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos
php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...
- php中常用的字符串查找函数strstr()、strpos()实例解释
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...
- C语言字符篇(二)字符串处理函数
字符串处理函数 1. 拷贝 strcpy 2. 追加 strcat #include <string.h> char *strcpy(char *dest, const char ...
- C语言中常用的字符串处理函数总结
C语言中字符串处理函数备注 此文仅用于自己研究和记录 字符串处理函数 1. char *gets(char *s); #include<stdio.h> 功能: 从标准输入读入字符,并保存 ...
- c语言字符数组与字符串的使用详解
转自:http://www.jb51.net/article/37456.htm 1.字符数组的定义与初始化字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素.char str[10]={ ...
- C语言中常用的字符串操作函数
程序开头要声明 #include <string.h> 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char ...
- Excel怎样从一串字符中的某个指定“字符”前后截取字符及截取字符串常用函数
怎么样可以从一串字符中的某个指定位置的前或后截取指定个数的字符. 如:12345.6789,我要截取小数点前(或后)的3个字符.怎么样操作, 另外,怎么样从右边截取字符,就是和left()函数相反的那 ...
- C语言字符数组和字符串
用来存放字符的数组称为字符数组,例如: char a[10]; //一维字符数组 char b[5][10]; //二维字符数组 char c[20]={'c', ' ', 'p', 'r', 'o' ...
随机推荐
- Android学习记录帖
1.OptionMenu的创建过程类图 2. OptionMenu的isActionBarMenu 在PhoneWindow的preparePanel中会根据设定的FEATURE来给变量isActio ...
- Start activity with App Chooser or not ?
启动一个Activity,可以直接使用Intent,例如: Intent intent = new Intent(Intent.ACTION_SEND); ... startActivity(inte ...
- cookie乱码处理 示例
package com.log; import java.io.IOException; import java.net.URLEncoder; import java.util.ArrayList; ...
- 关于Android Studio中的一个小问题——R文件引用Id失败
错误情况: 今天使用AS建立了一个新的EmptyProject,结果出现错误 setContentView(R.layout.activity_main); R文件的引用Id失败.真的是莫名奇妙... ...
- Mac下配置apach服务
有的时候,我们需要在内网工作组中分享一些文件或是后台接口没有及时给出,你又想要模拟真实数据,直接在项目里创建plist也可以做到这种需求,但难免让工程变得冗余且看起来比较Low.这个时候就看出配置本地 ...
- Prestashop-1.6.1.6-zh_CN (Openlogic CentOS 7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: prestashop1.6.1.6 commercial content management ecommerce open-source 简体中文 ...
- HTML5开发,背后的事情你知道吗?
现在的H5越来越受到企业或者是开发者的一个大力的追捧,已经成为网络推广必不可少的一个使用的工具,相信还有很多朋友现在都不知道H5是个什么东西,本文将为大家讲的是关于H5一些分类的问题,让你进一步的去学 ...
- java IO流——字符流
一.概述 流的概念: 流是个抽象的概念,是对输入输出设备的抽象,Java程序中,对于数据的输入/输出操作都是以“流”的方式进行.设备可以是文件,网络,内存等. 流具有方向性,至于是输入流还是输出流则是 ...
- 【LOJ6029】「雅礼集训 2017 Day1」市场(线段树裸题)
点此看题面 大致题意: 维护序列,支持区间加法,区间除法(向下取整),区间求\(min\)和区间求和. 线段树维护区间除法 区间加法.区间求\(min\)和区间求和都是线段树基本操作,因此略过不提. ...
- 深入浅出Nginx
深入浅出Nginx 文章源自zfz_linux_boy 前言 Nginx是一款轻量级的Web服务器.反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用. 上图基 ...