用 法: char *strchr(char *str, char c);
功 能: 在一个串中查找给定字符的第一个匹配之处(Locate first occurrence of character in string, 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 to retrieve a pointer to the end of a string.)
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char string[15];
char *ptr, c = 'r';
strcpy(string, "This is a string");
ptr = strchr(string, c);
if (ptr)
printf("The character %c is at position: %d/n", c, ptr-string);
else
printf("The character was not found/n");
return 0;
}
7、函数名: strcspn
用 法: int strcspn(char *str1, char *str2);
功 能: 在第一个串中查找包含任何第二个串给定字符集内容的位置(Get span until character in string, Scans str1 for the first occurrence of any of the characters that are part of str2, returning the number of characters of str1 read before this first occurrence. The search includes the terminating null-characters, so the function will return the length of str1 if none of the characters of str2 are found in str1.)
程序例:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
char *string1 = "1234567890";
char *string2 = "747D18";
int length;
length = strcspn(string1, string2); //length=0, match letter is 1
printf("Character where strings intersect is at position %d/n", length);
return 0;
}
8、函数名: strspn
用 法: int strspn(char *str1, char *str2);
功 能: 在串中查找指定字符集出现在串的子集的长度(Get span of character set in string, Returns the length of the initial portion of str1 which consists only of characters that are part of str2)
程序例:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
char *string1 = "1234567890";
char *string2 = "23DC81";
int length;
length = strspn(string1, string2); //length=3, the initial portion '123' cnsists of characters that are pare of string2
printf("Character where strings differ is at position %d/n", length);
return 0;
}
9、函数名: strpbrk
功 能: 在串中查找给定字符集中的字符
用 法: char *strpbrk(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string1 = "abcdefghijklmnopqrstuvwxyz";
char *string2 = "onm";
char *ptr;
ptr = strpbrk(string1, string2);
if (ptr)
printf("strpbrk found first character: %c/n", *ptr);
else
printf("strpbrk didn't find character in set/n");
return 0;
}
C++ Primer Plus 第6版 中文版 清晰有书签PDF+源代码
将C语言梳理一下,分布在以下10个章节中:
Linux-C成长之路(十):其他高级议题