// crt_strtok.c /* In this program, a loop uses strtok * to print all the tokens (separated by commas * or blanks) in the string named "string". */#include <string.h> #include <stdio.h>char string[] = "A string\tof ,,tokens\nand some more tokens"; char seps[] = " ,\t\n"; char *token;int main( void ) { printf( "Tokens:\n" ); /* Establish string and get the first token: */ token = strtok( string, seps ); while( token != NULL ) { /* While there are tokens in "string" */ printf( " %s\n", token ); /* Get next token: */ token = strtok( NULL, seps ); } } Output Tokens: A string of tokens and some more tokens
int i=0;
while(str.getlength>0)这个条件不一定完全正确哦
{
str1=str.left(str.find(" "));
str=str.right(str.getlength()-str.find(" ")-1);
a[i]=atoi(str1);
i++;
}
#include<strstream>
using namespace std;
int main(int argc,char* argv[])
{ char sz[]="111 222 3 44 55 666";
istrstream is( sz );
int x=0;
while(is>>x){
cout<<x<<"\n";
}
return 0;
}
int foo( int buf[], int size, char str[] )
{
int cnt = 0;
if( cnt >= size )
return cnt;
buf[0] = 0;
while( *str++ )
{if( *str >= '0' && *str <= '9' )
{
buf[cnt] = buf[cnt] * 10 + *str - '0';
}
else if( cnt < size - 1 )
cnt ++;
}
return cnt;
}
试试strtok
CString::Find,CString::Left
char* token;
char seps[]=" ";
int data[100];
int count=0;
token=strtok(str,seps);
data[count++]=atoi(token);
while(token!=NULL)
{
data[count++]=atoi(token);
token=strtok(NULL,seps);
}
CString::Find,CString::Left
-------------------------------------
我先去试试, 没使过
/* In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/#include <string.h>
#include <stdio.h>char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;int main( void )
{
printf( "Tokens:\n" );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}
Output
Tokens:
A
string
of
tokens
and
some
more
tokens
#include <iostream>
#include <strstream>
using namespace std;int main(void)
{
char *str = "12 23 45";
int arr[3];
int j = 0;
int temp = 0;
istrstream in(str);
for(j = 0; j < 3; j++)
{
in >> temp;
arr[j] = temp;
} for(j = 0; j < 3; j++)
{
cout << arr[j] << "\t";
}
cout << endl;
return 0;
}
#include <stdlib.h>char* trim_num( int *buf, char str[] )
{
int started = 0;
*buf = 0; while( 1 )
{ if( *str >= '0' && *str <= '9' )
{
if( started == 0 )
{
*buf = *str - '0';
started = 1;
}
else
{
*buf = *buf * 10 + *str - '0';
}
}
else if( *str == '\0' )
{
if( started == 0 )
{
return NULL;
}
else
{
return str;
}
}
else
{
if( started != 0 )
{
return str;
}
}
str ++;
} return str;
}char *str = "12 1441,42,,i4432 424";int main( int argc, char *argv[] )
{
int num;
char* cur = str;
while( cur = trim_num( &num, cur ) )
{
printf( "%d\n", num );
}
system( "PAUSE" );
return (0);
}
输出:
12
1441
42
4432
424
请按任意键继续 . . .
如果到达字符串尾则返回NULL
data是char *型的`