69,381
社区成员
发帖
与我相关
我的任务
分享
/*-
* Copyright (C), 1988-2010, KOBUS Co., Ltd.
*
* $Id: hello.c,v 1.2 2010/08/30 09:48:56 mymtom Exp $
*
* $Log: hello.c,v $
* Revision 1.2 2010/08/30 09:48:56 mymtom
* *** empty log message ***
*
* Revision 1.1 2010/08/30 09:46:30 mymtom
* Initial revision
*
*/
#ifndef lint
static const char rcsid[] =
"$Id: hello.c,v 1.2 2010/08/30 09:48:56 mymtom Exp $";
#endif /* not lint */
/**
* @file strsep.c
* @brief
*/
#include <stddef.h>
char *
k_strsep(char **stringp, const char *delim)
{
char *s;
const char *spanp;
int c, sc;
char *tok;
if ((s = *stringp) == NULL)
return (NULL);
for (tok = s;;) {
c = *s++;
spanp = delim;
do {
if ((sc = *spanp++) == c) {
if (c == 0)
s = NULL;
else
s[-1] = 0;
*stringp = s;
return (tok);
}
} while (sc != 0);
}
/* NOTREACHED */
}
#include <stdio.h>
int
main(int argc, char *argv[])
{
char buf[] =
"02|20100823|135033|000028|||02401|"
"02001|02402|00183|00010|0101141417|"
"9556690876543224|00|1|7410.00||||*";
char *tok;
const char *sep = "|";
char *str = buf;
while ((tok = k_strsep(&str, sep)) != NULL) {
printf("[%s]\n", tok);
}
return 0;
}
#include<stdio.h>
int main()
{
char str[1000] = "02|20100823|135033|000028|||02401|02001|02402|00183|00010|0101141417|9556690876543224|00|1|7410.00||||*";
//printf("%s\n",str);
int tmp[20]={0};
//const char *del = ",";
const char *del = "|";
int i=0,j=0;
//memset(tmp,0x00,sizeof(tmp));
char *s;
s=strtok(str, del);
while(s != NULL)
{
//strcpy(tmp,s);
tmp[i]=i;
printf("s =[%s] tmp=[%d]\n",s,tmp[i]);
i++;
s = strtok(NULL, del);
}
system("pause");
return 0;
}
/* 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;
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 );
}
}
Output
A string of ,,tokens
and some more tokens
Tokens:
A
string
of
tokens
and
some
more
tokens