C语言库函数的实战之一

本文介绍了C语言中几个常用的字符串处理函数,包括strtok()用于分割字符串,strrchr()用于查找最后一个字符出现的位置,strpbrk()用于查找两个字符串中首次出现相同的字符位置,以及strlen()用于获取字符串长度。
//-1:strtok()函数
#include<stdio.h>
#include<string.h>

int main(void)
{
	char buf[]="hello#world#today";//即将被分割的字符串
	char *temp = strtok(buf,"#");
	while(temp)
	{
		printf("%s ",temp);
		temp = strtok(NULL,"#");
	}
	printf("\n");
return 0;

}

  

//-2:strrchr()
#include <string.h>
#include <stdio.h>
void main()
{
    char * pCh = "www.inkcool.com";
    char * pFind = strrchr(pCh, '.');//找出点.最后一次出现在pCH的位置
    if ( pFind != NULL)
    {
        printf("%s", pFind);    //可以直接printf(pFind);printf("/n");左边的表达式是合二为一的表达方法;
    }
	printf("\n");
}

  

//-3:strpbrk():返回两个字符串中首个相同字符的位置
#include<stdio.h>
#include<string.h>
int main(void){
  char* s1 = "http://see.xidian.edu.cn/cpp/u/xitong/";//在C语言中,用指针形式来定义未知长度的字符串
  char* s2 = "see";
  char* p = strpbrk(s1,s2);
  if(p){
    printf("The result is: %s\n",p);  
  }else{
//-4:strlen()返回字符串的长度
#include<stdio.h>
#include<string.h>

int main(void)
{
	char* str1 = "zheng chaobing";
    printf("strlen(str1)=%d",strlen(str1));

	return 0;

}

  

printf("Sorry!\n"); } return 0; }

  

转载于:https://www.cnblogs.com/ZZiz/p/7622212.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值