当前位置: 首页 > news >正文

strtok的使用

Strtok:原型char *strtok(char s[], const char *delim); s为要分解的字符,delim为分隔符字符(如果传入字符串,则传入的字符串中每个字符均为分割符)。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。在头文件#include<string.h>中。strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。如果要保持原字符串的完整,可以使用strchr和sscanf的组合等。
返回:拆出来字符串首地址
第1个参数:第一次传要拆的串,第二次传NULL(表示向下拆)
第2个参数:用哪些字符做分割符,将分割符改成\0返回

#include <string.h>
void read_all_words1(char (*words)[1000],int *pCnt){char str[1000000] = {0};char * p = str;gets(str);while(p = strtok(p," ")){strcpy(words[(*pCnt)],p);(*pCnt)++;p = NULL;}
}
void read_all_words(char (*word)[1000],int *pCnt) {do{scanf("%s",word[(*pCnt)]);(*pCnt)++;}while(getchar()!='\n');
}
int main(){char words[1000][1000] = {0};int cnt = 0;//read_all_words(words,&cnt);read_all_words1(words,&cnt);for(int i = 0;i<cnt;i++){printf("%s ",words[i]);}
}
#include<stdio.h>
#include<string.h>
enum status{BLANK,CHAR};
int fun(char str[]){int words = 0;char temp[100] = {" "};strcat(temp,str);strcat(temp," ");for(int i = 1;i<= strlen(temp)-2;i++){if(temp[i] != ' ' && temp[i-1] == ' '){words ++;}}return words;
}
int main(){char * p = "i am a boy";printf("%d",fun(p));
}
#include<stdio.h>
#include<string.h>
enum status{BLANK,CHAR};
int fun(char str[]){int words = 0;char temp[100] = {" "};strcat(temp,str);strcat(temp," ");for(int i = 1;i<= strlen(temp)-1;i++){if(temp[i] != ' ' && temp[i-1] == ' '){words ++;}}return words;
}
int main(){char * p = "i am a b";printf("%d",fun(p));
}
http://www.lryc.cn/news/294957.html

相关文章:

  • 0206作业
  • 数据结构-栈
  • CentOS7搭建k8s-v1.28.6集群详情
  • Android实现底部导航栏方法(Navigation篇)
  • python 爬虫篇(1)---->re正则的详细讲解(附带演示代码)
  • (超详细)10-YOLOV5改进-替换CIou为Wise-IoU
  • Java-并发高频面试题-2
  • Windows安装Redis
  • Nicn的刷题日常之 有序序列判断
  • 1、将 ChatGPT 集成到数据科学工作流程中:提示和最佳实践
  • vite+vue3发布自己的npm组件+工具函数
  • 嵌入式软件bug分析基本要求
  • 【C/C++ 17】继承
  • 解决Linux Shell脚本错误:“/bin/bash^M: bad interpreter: No such file or directory”
  • idea创建spring项目
  • 【UE 材质】扇形材质
  • 【react native】ScrollView的触摸事件与TouchableWithoutFeedback的点击事件冲突
  • 鸿蒙内核框架
  • 幻兽帕鲁专用服务器,多人游戏(专用服务器)搭建
  • 7000字详解Spring Boot项目集成RabbitMQ实战以及坑点分析
  • AJAX-认识URL
  • 国图公考:公务员面试资格复审需要准备什么?
  • 爬虫实战--人民网
  • 【Arduino】LGT8F328 UNO R3编译上传
  • Python进阶----在线翻译器(Python3的百度翻译爬虫)
  • ArcGISPro中Python相关命令总结
  • 2024年混合云:趋势和预测
  • c++入门学习④——对象的初始化和清理
  • Java-spring注解的作用
  • Allegro如何把Symbols,shapes,vias,Clines,Cline segs等多种元素一起移动