C语言如何从特殊的地方截取字符串
例如:
有字符串:'VL04a+80ms+VL04b+10ms+VL04c'
我需要将字符串从有加号的地方截开变成5个字符串
那么5个字符串分别是:
'VL04a'
'80ms'
'VL04b'
'10ms'
'VL04c'
请懂的朋友解答下,谢谢!! c语言 字符串 截取
[解决办法]
#include <string.h>
#include <stdio.h>
int main()
{
char text[] = "VL04a+80ms+VL04b+10ms+VL04c";
char* d = "+";
char* p = strtok(text, d);
while(p != NULL)
{
printf("%s\n", p);
p = strtok(NULL, d);
}
return 0;
}
#include <string.h>
#include <stdio.h>
char string[] = "VL04a+80ms+VL04b+10ms+VL04c";
char seps[] = "+";
char *token;
void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* 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 );
}
}