C语言字符串操作函数总结(2)

用  法: 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++ Primer 之构造函数陷阱

读C++ Primer 之智能指针

读C++ Primer 之句柄类

将C语言梳理一下,分布在以下10个章节中:

Linux-C成长之路(一):Linux下C编程概要

Linux-C成长之路(二):基本数据类型

Linux-C成长之路(三):基本IO函数操作

Linux-C成长之路(四):运算符

Linux-C成长之路(五):控制流

Linux-C成长之路(六):函数要义

Linux-C成长之路(七):数组与指针

Linux-C成长之路(八):存储类,动态内存

Linux-C成长之路(九):复合数据类型

Linux-C成长之路(十):其他高级议题

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/03505b8c7de9c1e1f2efc11eb60093c4.html