点击(此处)折叠或打开
#include<iostream> #include<string.h> #include<string> using namespace std; int main() { char str[] = "Hello world"; char *token = strtok(str," "); //cout << token << endl; while (token != NULL) { string s(token); token = strtok(NULL," "); cout << s << endl; } return 0; } 输出: [root@localhost c++]# ./a.out Hello world 应用:删除重复字符串点击(此处)折叠或打开
#include<iostream> #include<map> #include<string.h> #include<string> using namespace std; int main() { char str[200] = {0}; char strbak[200] = {0}; char *token = NULL; map<string,int> wordMap; string output; int i = 0,j = 0; gets(str); for (int i = 0;str[i] != '\0';i++) { if (str[i] == ',' || str[i] == ' ') { strbak[j] = ' '; j++; } else { strbak[j] = str[i]; j++; } } strbak[j] = '\0'; strncpy(str,strbak,sizeof(strbak)+1); token = strtok(str," "); while (token != NULL) { string s(token); wordMap[s]++; token = strtok(NULL," "); } int count = 0; token = strtok(strbak," "); while (token != NULL) { string s(token); if (wordMap[s] >= 1) { if (count == 0) { output = output+s; wordMap[s] = 0; } else { output = output+" "+s; wordMap[s] = 0; } count++; } token = strtok(NULL," "); } cout << output<< endl; return 0; } 运行结果: [root@localhost c++]# ./a.out i love love zhouyi i love zhouyi <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> 阅读(67) | 评论(0) | 转发(0) | 0上一篇:linux内核编程4部曲之四:模块编程
下一篇:strncpy的实现
相关热门文章 test123编写安全代码——小心有符号数...使用openssl api进行加密解密...一段自己打印自己的c程序...彻底搞定C语言指针详解-完整版... 给主人留下些什么吧!~~ 评论热议