char *strtok(char s[], const char *delim);
输出:
[root@localhost c++]# ./a.out
Hello
world
应用:删除重复字符串
运行结果:
[root@localhost c++]# ./a.out
i love love zhouyi
i love zhouyi
点击(此处)折叠或打开
- #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) |
给主人留下些什么吧!~~
评论热议

1万+

被折叠的 条评论
为什么被折叠?



