将字符串解析为数组 [英] parsing string into an array

查看:86
本文介绍了将字符串解析为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串解析为数组。我在网上找到了以下代码来解析字符串并将其打印出来。

结果

正是我想要的:


char * pch;

pch = strtok(缓冲区,"" );

while(pch!= NULL)

{

printf("%s\ n,pch);

pch = strtok(NULL,&,;,。");

}


要将输出存储到字符串数组中,我创建了一个二维的

数组并将输出复制到数组:


char输出[5] [55];

char * pch;

int i = 0;

pch = strtok(str,"");

while(pch != NULL)

{

strcpy(output [i],pch);

printf("%s \ n", pch);

printf(" output [%d] is%s。\ n" ;; i,output [i ++]);

pch = strtok(NULL ,,,;

}


这不起作用。输出数组中的内容与

输出(屏幕上)完全不同。应该怎么做?

I would like to parse a string into an array. I found on the net
the following codes which parse a string and print it. The result
is exactly what I want:

char * pch;
pch = strtok (buffer," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
}

To store the output to a string array, I created a two-dimensional
array and copied the output to the array:

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n";i,output[i++]);
pch = strtok (NULL, " ,.");
}

This doesn''t work. What''s in the output array is nothing like the
output (on screen). How should it be done?

推荐答案

在文章< lb ****************** ***@twister.southeast.rr.com> ,

John Smith< js **** @ company.com>写道:
In article <lb*********************@twister.southeast.rr.com> ,
John Smith <js****@company.com> wrote:
printf(" output [%d] is%s。\ n" ;; i,output [i ++]);
printf("output[%d] is %s.\n";i,output[i++]);




如果我没记错,这会使用未定义的行为。

后期增量可以在

序列点之间的任何时间完成,所以它不是定义对i的第一个

引用是否在

增量之前或之后取值。


它也是一种语法错误将分号放在中间

语句中。该行根本不应该编译。

-

''ignorandus(拉丁语):值得不知道''

- 自我指涉期刊



If I recall correctly, this uses undefined behaviour.
The post increment may be done at any time between the
sequence points, so it is not defined whether the first
reference to i has its value taken before or after the
increment.

It also is a syntax error to have the semicolon in the middle
of the statement. That line should not compile at all.
--
''ignorandus (Latin): "deserving not to be known"''
-- Journal of Self-Referentialism


Walter Roberson写道:
Walter Roberson wrote:
文章< lb ********** ***********@twister.southeast.rr.com> ,
John Smith< js **** @ company.com>写道:

In article <lb*********************@twister.southeast.rr.com> ,
John Smith <js****@company.com> wrote:

printf(" output [%d] is%s。\ n" ;; i,output [i ++]);
printf("output[%d] is %s.\n";i,output[i++]);



如果我没记错,这会使用未定义的行为。
后期增量可能在
序列点之间的任何时间完成,所以没有定义是否第一个
对...的引用在
增量之前或之后取值。

在语句的中间部分使用分号也是语法错误。该行根本不应该编译。


If I recall correctly, this uses undefined behaviour.
The post increment may be done at any time between the
sequence points, so it is not defined whether the first
reference to i has its value taken before or after the
increment.

It also is a syntax error to have the semicolon in the middle
of the statement. That line should not compile at all.




感谢您的回复。那个分号是一个错字。


实际上,我也尝试了以下(单独的i ++)并且

也没用。


char输出[5] [55];

char * pch;

int i = 0;

pch = strtok(str,"");

while(pch!= NULL)

{

strcpy(output [i] ,pch);

printf("%s \ nn",pch);

printf(" output [%d] is%s。\ n" ;,i,输出[i]);

i ++;

pch = strtok(NULL,",。");

}



Thanks for the reply. That semicolon was a typo.

Actually, I had also tried the following (a separate i++) and it
did not work either.

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n",i,output[i]);
i++;
pch = strtok (NULL, " ,.");
}





John Smith写道:


John Smith wrote:

其实我也试过以下(单独的i ++)它也没有用。

char输出[5] [55];
char * pch;
int i = 0;
pch = strtok(str,"");
while(pch!= NULL)
{
strcpy(output [i],pch);
printf("%s \ n",pch);
printf(" output [%d] i s%s。\ n",i,output [i]);
i ++;
pch = strtok(NULL," ,。;
}

Actually, I had also tried the following (a separate i++) and it
did not work either.

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n",i,output[i]);
i++;
pch = strtok (NULL, " ,.");
}




这里提供的代码似乎没问题。你是什​​么

的意思是什么都不起作用。

-

Al Bowers

坦帕,Fl USA

mailto: xa******@myrapidsys.com (删除x发送电子邮件)
http://www.geocities.com/abowers822 /



The code provided here seems to be ok. What do you
mean by "did not work either".
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


这篇关于将字符串解析为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆