学会用函数strtok,功能:函数返回字符串str1中紧接“标记”的部分的指针, 字符串str2是作为标记的分隔符。如果分隔标记没有找到,函数返回NULL。为了将字符串转换成标记,第一次调用str1 指向作为标记的分隔符。之后所以的调用str1 都应为NULL。
例如:
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL
result = strtok( str, delims )
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
以上代码的运行结果是:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
char str[1005];
char *p;
while(cin>>str)
{
int a[1005];
int i,t=0;
p=strtok(str,"5");
while(p)
{
a[t++]=atoi(p); //将字符串转化为整型
// cout<<p<<' ';
p=strtok(NULL,"5");
}
sort(a,a+t);
for(i=0;i<t;i++){
cout<<a[i];
if(i==t-1) cout<<endl;
else cout<<' ';
}
}
return 0;
}
浙公网安备 33010602011771号