字符串截取

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

 

//字符串截取strtok会破坏源字符串,用\0替换截取的标志位

int main0101()

{

  char ch[]="www.itcast.cn";

 

  char*p=strtok(ch,".");//www\0itcast.cn

  printf("%s\n",p);//www

 

  p=strtok(NULL,".");//www\0itcast\0cn

  printf("%s\n",p);//itcast

 

  p=strtok(NULL,".");

  printf("%s\n",p);//cn

  

  return EXIT_SUCCESS;

}

 

int main0102(void)

{

  char ch[]="123456@qq.com";

  char str[100]={0};

//字符串备份

  strcpy(str,ch);

  

  char*p=strtok(str,"@");

  printf("%s\n",p);//123456

  

  p=strtok(NULL,"."); 

  printf("%s\n",p);//qq

  

  return 0;

}

 

int main(void)

{

  //char ch[] = "nichousha\nchounizadi\nzaichouyigeshishi\nduibuqidagewocuole\nguawazi\n";

  char ch[] = "你瞅啥\n瞅你昨地\n再瞅一个试试\n对不起大哥我错了\n瓜娃子\n";

  char*p=strtok(ch,"\n");

  while(p)

  {

    printf("%s\n",p);

    p=strtok(NULL,"\n");

  }

  return 0;

//结果

 

 

}

posted @ 2020-09-04 22:02  wh19991213  阅读(123)  评论(0编辑  收藏  举报