代码如下:(未曾测试过)
#include <stdio.h>
#include <string.h>
#include <assert.h>
void split001( const char* str, const char chr )
{
const char* p1 = str;
for( const char* p2; (p2=strchr(p1,chr))!=0; p1=p2+1 )
{
printf( ""%.*s"n", p2-p1, p1 );
}
printf( ""%s"n", p1 );
}
void split002( const char* str, const char* substr )
{
assert( *substr );
const size_t substrlen = strlen(substr);
const char* p1 = str;
for( const char* p2; (p2=strstr(p1,substr))!=0; p1=p2+substrlen )
{
printf( ""%.*s"n", p2-p1, p1 );
}
printf( ""%s"n", p1 );
}
void split003( const char* str, const char* substr )
{
assert( *substr );
const char* p1 = str;
for( const char* p2; p2=p1+strcspn(p1,substr), *p2; p1=p2+1 )
{
printf( ""%.*s"n", p2-p1, p1 );
}
printf( ""%s"n", p1 );
}
void split004( const char* str, const char* substr )
{
assert( *substr );
const char* p1 = str;
for( const char* p2; p2=p1+strcspn(p1,substr), *p2; p1=p2+strspn(p2,substr) )
{
printf( ""%.*s"n", p2-p1, p1 );
}
printf( ""%s"n", p1 );
}
int main( void )
{
split001( "123@456@789", '@' );
printf( "---n" );
split002( "123@#456@#789", "@#" );
printf( "---n" );
split003( "123@456#789@#0", "@#" );
printf( "---n" );
split004( "123@456#789@#0", "@#" );
printf( "---n" );
return 0;
}
3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/