char *strtok(char s[], const char *delim);
-
#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
阅读(1538) | 评论(0) | 转发(0) |