求教C语言处理字符串的问题

smallMage 2010-08-23 02:53:23
char str[100] = "02|20100823|135033|000028|||02401|02001|02402|00183|00010|0101141417|9556690876543224|00|1|7410.00||
||*";
char tmp[20][100];
//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[i],s);
printf("s =[%s] tmp=[%s] \n",s,tmp);
i++;
s = strtok(NULL, del);
}
tmp[0][ 02]
tmp[1][20100823]
tmp[2][135033]
tmp[3][000028]
tmp[4][02401]
tmp[5][02001]
tmp[6][02402]
tmp[7][00183]
tmp[8][00010]
tmp[9][0101141417]
tmp[10][9556690876543224]
tmp[11][00]
tmp[12][1]
tmp[13][7410.00]
tmp[14][ôo–]

空值没有出现,

...全文
210 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
smallMage 2010-08-30
  • 打赏
  • 举报
回复
非常感谢mymtom
赵4老师 2010-08-30
  • 打赏
  • 举报
回复
对字符串处理(string parse)问题
strtok、strsep、正则表达式都不是万能的;
只有‘有限状态自动机’是万能的。
参考《编译原理》词法分析之有限状态自动机。
mymtom 2010-08-30
  • 打赏
  • 举报
回复
输出为
[02]
[20100823]
[135033]
[000028]
[]
[]
[02401]
[02001]
[02402]
[00183]
[00010]
[0101141417]
[9556690876543224]
[00]
[1]
[7410.00]
[]
[]
[]
[*]
mymtom 2010-08-30
  • 打赏
  • 举报
回复

/*-
* 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;
}
smallMage 2010-08-29
  • 打赏
  • 举报
回复
char str[100] = "02|20100823|135033|000028|||02401|02001|02402|00183|00010|0101141417|9556690876543224|00|1|7410.00||
||*";
问题已经解决,我先把两个'|'之间补充一个 空格,然后再处理就OK了, 大家有没有更好的方法了,谢谢了
j=0;
memset( tmp_str, 0, sizeof(tmp_str) );
for( i=0; i<strlen(str); i++ )
{
if( str[i] == ',' && str[i+1] == ',' )
{
tmp_str[j] = str[i];
tmp_str[j+1] = ' ';
j += 2;
}
else
{
tmp_str[j] = str[i];
j ++;
}
}
如果我想取02401|02001|02402 这段字串,两个‘|’之间是不定长的,
smallMage 2010-08-23
  • 打赏
  • 举报
回复
楼上的兄弟两个"|"没有值,你这个就有问题了
bobo364 2010-08-23
  • 打赏
  • 举报
回复
#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;
}
smallMage 2010-08-23
  • 打赏
  • 举报
回复
问题已经解决,我先把两个'|'之间补充一个 空格,然后再处理就OK了, 大家有没有更好的方法了,谢谢了
j=0;
memset( tmp_str, 0, sizeof(tmp_str) );
for( i=0; i<strlen(str); i++ )
{
if( str[i] == ',' && str[i+1] == ',' )
{
tmp_str[j] = str[i];
tmp_str[j+1] = ' ';
j += 2;
}
else
{
tmp_str[j] = str[i];
j ++;
}
}
smallMage 2010-08-23
  • 打赏
  • 举报
回复
也就是说要在两个||之间插入一个空格,
smallMage 2010-08-23
  • 打赏
  • 举报
回复
问题是 tmp[3][000028]
tmp[4][02401]
"02|20100823|135033|000028|||

tmp[4]取值应该为空,
tmp[5]才应该为02401
赵4老师 2010-08-23
  • 打赏
  • 举报
回复
/* 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

ex_dijkstra 2010-08-23
  • 打赏
  • 举报
回复
你把char *s初始化一下试试。。。
输出到tmp[14][ôo–]就没有了么?还是报错了?
sdf_maxwit 2010-08-23
  • 打赏
  • 举报
回复
MaxWit魔鬼训练营技术讨论区:http://linux.chinaunix.net/bbs/forum-70-1.html
chjh0540237 2010-08-23
  • 打赏
  • 举报
回复
feimashenhua 2010-08-23
  • 打赏
  • 举报
回复
自己查一下函数strtok,是你分割字符串写的有误

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧