字符串,字符串和诅咒字符串 [英] Strings, Strings and Damned Strings

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

问题描述

我有一个int变量(总是<100)我要转换为两个字符的字符串,例如

如果myint = 1,则为
,mystr =" 01"

如果myint = 81,mystr =" 81"


目前我无法弄清楚如何干净利落地完成这项工作。

然后我希望将一堆这些字符串推入一个数组,例如:


typedef char LABEL [3];

LABEL mystrArray [100];


但是阅读了几个教程后,我仍然不清楚做字符串数组的最佳方法。在这种情况下,也没有

怎么做。


任何帮助都非常感谢!


欢呼,


Ben

解决方案



Ben写道:

我有一个int变量(总是<100),我想转换为两个字符串,例如

如果myint = 1,mystr =" 01"
如果myint = 81,mystr =" 81"

目前我无法弄清楚如何干净利落地做这件事。

然后我想推出一堆这些将字符串转换为数组,例如:

typedef char LABEL [3];
LABEL mystrArray [100];

但是阅读了几个教程我就是仍然没有更清楚的做出字符串数组的最佳方法在这种情况下,也没有怎么做。

任何帮助都非常感谢!

欢呼,

Ben




对于第一个问题,google sprintf并注意

格式化说明符。那里有一些东西可以指定要输出的字符串的宽度

,以及是否用

''0'填充该宽度或者空格。


你的第二个问题,就是这样做

char mystrArray [3] [100];


一切这需要一个char *参数,你想在其上操作

mystrArray的一个元素(即sprintf:

sprintf(mystrArray [0],< extra args> )

sprintf(mystrArray [1],< extra args>)

sprintf(mystrArray [2],< extra args>)

>


Ben写道:

我有一个int变量(总是< 100)我要转换为两个字符串,例如

如果myint = 1,mystr =" 01"
如果myint = 81,则mystr =" 81"

目前我无法弄清楚如何干净利落。


最简单的方法是使用sprintf()。但是,你也可以做
通过索引到查找表的转换,每个索引都是

字符串文字表示。对于较大的myint值,

你可以隔离每个数字的值,然后使用查找表。

然后我希望将这些字符串推入一个数组,例如:

typedef char LABEL [3];
LABEL mystrArray [100];

但是阅读了几个教程我还是不清楚关于做字符串数组的最佳方法在这种情况下,也不知道怎么做。




使用指向char和商店的数组(静态或动态)

通过malloc''将数组的每个成员的字符串设置为所需的

大小。如果你事先知道字符串的大小,并且如果他们在执行期间不会改变
,那么静态的2d数组可能会更简单。


"本" <是********* @ spam.me>在消息中写道

news:12 ************* @ corp.supernews.com ...

我有一个int变量(总是< 100)我想转换成两个
字符串,例如

如果myint = 1,mystr =" 01"
如果myint = 81,mystr =" 81"

目前我无法弄清楚如何干净利落地做到这一点。


sprintf()

可能你想要%02d对于你的格式说明符。

然后我希望将一堆这些字符串推入一个数组中,例如:

typedef char LABEL [3];
LABEL mystrArray [100];


看起来不错。除了我没有尖叫的typedef。看起来很像

就像一个宏。

但是阅读了几个教程后,我仍然没有更清楚地做出最好的方法来做一个数组字符串在这种情况下,也不知道怎么做。

任何帮助非常感谢!


向我们展示您的程序,演示您尝试过的内容以及

错误。

欢呼,



I have an int variable (always <100) that I want to convert to a two character string, e.g.

if myint = 1, mystr = "01"
if myint = 81, mystr = "81"

At the moment I can''t figure out how to do this cleanly.
Then I wish to push a bunch of these strings into an array, for example:

typedef char LABEL[3];
LABEL mystrArray[100];

But having read a couple of tutorials I am still no clearer on the best way to do an "array of strings" in this situation, nor
how to do it.

Any help much appreciated!

cheers,

Ben

解决方案


Ben wrote:

I have an int variable (always <100) that I want to convert to a two character string, e.g.

if myint = 1, mystr = "01"
if myint = 81, mystr = "81"

At the moment I can''t figure out how to do this cleanly.
Then I wish to push a bunch of these strings into an array, for example:

typedef char LABEL[3];
LABEL mystrArray[100];

But having read a couple of tutorials I am still no clearer on the best way to do an "array of strings" in this situation, nor
how to do it.

Any help much appreciated!

cheers,

Ben



For the first problem, google sprintf and pay attention to the
formatting specifiers. There''s something in there to specify the width
of the string to be output, and whether or not to pad that width with
''0''s or spaces.

Your second question, just do
char mystrArray[3][100];

Anything that requires an char* argument where you want to operate on
one of the elements of mystrArray (ie sprintf:
sprintf(mystrArray[0], <extra args> )
sprintf(mystrArray[1], <extra args> )
sprintf(mystrArray[2], <extra args> )


Ben wrote:

I have an int variable (always <100) that I want to convert to a two character string, e.g.

if myint = 1, mystr = "01"
if myint = 81, mystr = "81"

At the moment I can''t figure out how to do this cleanly.
The simplest way would be to use sprintf(). However, you can also do
the conversion by indexing into a lookup table, each index being a
string literal representation of itself. For larger values of myint,
you can isolate the value of each digit, and then use a lookup table.
Then I wish to push a bunch of these strings into an array, for example:

typedef char LABEL[3];
LABEL mystrArray[100];

But having read a couple of tutorials I am still no clearer on the best way to do an "array of strings" in this situation, nor how to do it.



Use an array, (either static or dynamic), of pointers to char and store
the strings by malloc''ing each member of the array to the required
size. If you know the sizes of strings in advance and if they won''t
change during execution, then a static 2d array might be simpler.


"Ben" <be*********@spam.me> wrote in message
news:12*************@corp.supernews.com...

I have an int variable (always <100) that I want to convert to a two
character string, e.g.

if myint = 1, mystr = "01"
if myint = 81, mystr = "81"

At the moment I can''t figure out how to do this cleanly.
sprintf()
Probably you want "%02d" for your format specifier.
Then I wish to push a bunch of these strings into an array, for example:

typedef char LABEL[3];
LABEL mystrArray[100];
That looks fine. Except I would not have a screaming typedef. It looks too
much like a macro.
But having read a couple of tutorials I am still no clearer on the best
way to do an "array of strings" in this situation, nor how to do it.

Any help much appreciated!
Show us your program that demonstrates what you have tried, and what went
wrong.
cheers,

Ben



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

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