C语言字符串函数
来源:互联网 发布:用友erp 软件界面 编辑:程序博客网 时间:2024/05/17 06:09
<string.h>中的字符串函数用法
1、strcspn
原型:size_t strcspn(const char *s1, const char *s2);
头文件:#include <string.h>
功能:顺序在字符串s1中搜寻与s2中任意字符相同的第一个字符,返回这个字符在s1中第一次出现的位置。
说明:(返回第一个出现的字符在s1中的下标值,亦即在s1中出现而s2中没有出现的子串的长度。)
简单地说,若strcspn()返回的数值为n,则代表字符串s1开头连续有n个字符都不含字符串s2的所有字符。
#include <stdio.h >
#include <string.h>
int main(void)
{
char *s1 = "Golden Gobal View";
char *s2 = "be"; // 等同于"eb", 它是不分先后顺序的
int n;
n = strcspn(s, r);
printf("The first char both in s1 and s2 is: %c/n", *(s + n));
printf("The first string both in s1 and s2 is: %s/n", s + n);
return 0;
}
运行结果:
The first char both in s1 and s2 is: e
The first string both in s1 and s2 is: en Gobal View
2、strspn
原型:size_t strspn (const char *s, const char * accept);
函数功能:在串中查找指定字符集的子集的第一次出现位置。
strspn()从参数s字符串的开头计算连续的字符,而这些字符都完全是accept 所指字符串中的字符。简单的说,若strspn()返回的数值为n,则代表字符串s开头连续有n个字符都是属于字符串accept内的字符。
返回值:返回字符串s开头连续包含字符串accept内的字符数目。如果accept中不存在字符串s的第一个字符,则返回0。
#include <stdio.h >
#include <string.h>
int main(void)
{
char *s1 = "1234567890";
char *s2 = "032DC81"; // 等同于"1230DC8", 不区分顺序
int length;
length = strspn(s1, s2);
printf("Character where strings differ is at position %d./n", length);
s2 = "052DC81";
length = strspn(s1, s2);
printf("Character where strings differ is at position %d./n", length);
s2 = "053DC81";
length = strspn(s1, s2);
printf("Character where strings differ is at position %d./n", length);
s2 = "053DC82";
length = strspn(s1, s2);
printf("Character where strings differ is at position %d./n", length);
s2 = "ERt5678";
length = strspn(s1, s2);
printf("Character where strings differ is at position %d./n", length);
s2 = "ERt567801";
length = strspn(s1, s2);
printf("Character where strings differ is at position %d./n", length);
return 0;
}
运行结果:
Character where strings differ is at position 3.
Character where strings differ is at position 2.
Character where strings differ is at position 1.
Character where strings differ is at position 0.
Character where strings differ is at position 0.
Character where strings differ is at position 1.
//函数实现 ++count;
int strspn(const char *s, const char *accept)
{
const char *p;
const char *a;
size_t count = 0;
for (p = s; *p != '/0'; ++p)
{
for (a = accept; *a != '/0'; ++a)
{
if (*p == *a)
{
break;
}
}
if (*a == '/0')
{
return count;
}
}
return count;
}
- C语言字符串函数
- C语言字符串函数
- C语言字符串函数
- C语言字符串函数
- C语言字符串函数
- C语言字符串函数
- c语言 字符串函数
- C语言字符串函数
- c语言字符串函数
- C语言字符串函数
- c语言字符串函数
- C语言字符串函数
- C语言字符串函数
- c语言字符串函数
- c语言字符串函数
- C语言字符串函数
- C语言-----字符串函数
- C语言字符串函数
- 生成图片并发布的Servlet
- PL/SQL --> DML 触发器
- oracle优化:避免全表扫描
- 感觉
- Thinking In Mainframe开博
- C语言字符串函数
- PL/SQL --> INSTEAD OF 触发器
- 动态库学习积累
- 汇编
- COM字符转换函数编译问题
- 面对大公司和小公司,应届毕业生如何选择
- C语言编写飞机票系统
- 用面向对象思想分析linux字符设备驱动开发
- 寂寞的季节 落寞的圣诞节