By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Strings, Strings and Damned Strings

P: n/a
Ben
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
Jun 22 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a

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> )

Jun 22 '06 #2

P: n/a
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.

Jun 22 '06 #3

P: n/a
"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

Jun 22 '06 #4

P: n/a
"Dann Corbit" <dc*****@connx.com> wrote in message
news:e7**********@nntp.aioe.org...

Maybe something like this:

#include <stdio.h>
#include <string.h>

typedef char Label[3];

int main(void)
{
size_t i;
Label mystrArray[100]={0};

for (i = 0; i < 100; i++)
sprintf(mystrArray[i], "%02d", i);

for (i = 0; i < 100; i++)
puts(mystrArray[i]);

return 0;
}
Jun 22 '06 #5

P: n/a
Ben
Dann Corbit wrote:
"Dann Corbit" <dc*****@connx.com> wrote in message
news:e7**********@nntp.aioe.org...

Maybe something like this:

#include <stdio.h>
#include <string.h>

typedef char Label[3];

int main(void)
{
size_t i;
Label mystrArray[100]={0};

for (i = 0; i < 100; i++)
sprintf(mystrArray[i], "%02d", i);

for (i = 0; i < 100; i++)
puts(mystrArray[i]);

return 0;
}


sprintf solves the first problem, but the array is still confusing me...here's the actual code:
char *labelArray [(4*MAX2*MAX2)+1];

int mkLabels(void) {

char blkid[3],colid[3],rowid[3],digit[3];
int i,j,colnum,rownum;
pCELL pointer = mArray[0][0][0][0];

for (i=0;i<n4;i++) {

printf("Making column labels for square %d\n",i);

char alabel[6] = "a";
char blabel[6] = "b";
char clabel[6] = "c";
char dlabel[6] = "d";

sprintf(blkid, "%02d",pointer->block);

int sindex = ilookup(pointer->symbol); /*printf("sindex is %d\n",sindex);*/
sprintf(digit, "%02d",sindex);

/* For columns and rows we have to do some annoying mapping to a two-digit identifier - mArray's fault */
colnum = (pointer->column)+((pointer->block%n)*n);
sprintf(colid, "%02d",colnum);

rownum = (pointer->row)+((pointer->block/n)*n);
sprintf(rowid, "%02d",rownum);
printf("blkid is %s, colid is %s, rowid is %s, digit is %s\n",blkid,colid,rowid,digit);

strcat(alabel,rowid); strcat(alabel,colid);
labelArray[i] = alabel;

strcat(blabel,digit); strcat(blabel,rowid);
labelArray[i+n4] = blabel;

strcat(clabel,digit); strcat(clabel,colid);
labelArray[i+n4+n4] = clabel;

strcat(dlabel,digit); strcat(dlabel,blkid);
labelArray[i+n4+n4+n4] = dlabel;

1) printf("alabel is %s, blabel is %s, clabel is %s, dlabel is %s\n",alabel,blabel,clabel,dlabel);

pointer = pointer->next_in_puzzle;
}

2) printf("\nContents of labelArray:\n");
for (j=0;j<4*n4;j++) {
printf("%s ",labelArray[j]);
}
printf("\n\n");

return 0;

}

This compiles and runs ok but doesn't work as required since each update to alabel, blabel etc changes those objects that all
the pointers point to, so I am not storing new labels just overwriting the same ones (the print contents at 2) confirms that).
Note I want the whole structure labelArray to be available globally which is another problem with the code above.

So I need to do something differently, perhaps the 2d array, but I couldn't get that to compile. At least the values at 1) are
correct, just the structure is wrong.

Jun 22 '06 #6

P: n/a

Ben wrote:
char *labelArray [(4*MAX2*MAX2)+1];
<snip>
This compiles and runs ok...


No, it doesn't. The very first lines references MAX2,
which isn't defined.

Jun 22 '06 #7

P: n/a
Ben
Bill Pursell wrote:
Ben wrote:
char *labelArray [(4*MAX2*MAX2)+1];


<snip>
This compiles and runs ok...


No, it doesn't. The very first lines references MAX2,
which isn't defined.


It didn't have a main() or include any headers either, but most people should have no problem working out the value of MAX2 is
irrelevant to the problem.
Jun 22 '06 #8

P: n/a
In article <12*************@corp.supernews.com>,
Ben <be*********@spam.me> wrote:
Bill Pursell wrote:
Ben wrote:
char *labelArray [(4*MAX2*MAX2)+1];


<snip>
This compiles and runs ok...


No, it doesn't. The very first lines references MAX2,
which isn't defined.


It didn't have a main() or include any headers either, but most people
should have no problem working out the value of MAX2 is
irrelevant to the problem.


Yes, but that's not how this newsgroup works.

http://en.wikipedia.org/wiki/Aspergers

Jun 22 '06 #9

P: n/a
Ben <be*********@spam.me> writes:
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.


You've already received a number of suggestions to use
sprintf(). This is one good way.

For a problem as simple as this, though, you can easily do it by
hand, if you like:
mystr[0] = myint / 10 + '0';
mystr[1] = myint % 10 + '0';
--
Go not to Usenet for counsel, for they will say both no and yes.
Jun 22 '06 #10

P: n/a
In article <87************@benpfaff.org>,
Ben Pfaff <bl*@cs.stanford.edu> wrote:
Ben <be*********@spam.me> writes:
I have an int variable (always <100) that I want to convert to a two character string, e.g. if myint = 1, mystr = "01"

For a problem as simple as this, though, you can easily do it by
hand, if you like:
mystr[0] = myint / 10 + '0';
mystr[1] = myint % 10 + '0';


mystr[2] = 0;

double-quoted strings are null terminated...
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Jun 22 '06 #11

P: n/a
"Ben" <be*********@spam.me> wrote in message
news:12*************@corp.supernews.com...
[snip]
This compiles and runs ok but doesn't work as required since each update
to alabel, blabel etc changes those objects that all the pointers point
to, so I am not storing new labels just overwriting the same ones (the
print contents at 2) confirms that). Note I want the whole structure
labelArray to be available globally which is another problem with the code
above.

So I need to do something differently, perhaps the 2d array, but I
couldn't get that to compile. At least the values at 1) are correct, just
the structure is wrong.


What is the exact problem that you are trying to solve?
Jun 24 '06 #12

P: n/a
Ben
Dann Corbit wrote:
What is the exact problem that you are trying to solve?


Well, I've now worked around the problem (still without knowing how to build an array of strings). But it was part of a sudoku
solver. I was incorporating someone else's program for solving exact cover problems and had to remap some data.
Jun 24 '06 #13

P: n/a
On 2006-06-24, Ben <be*********@spam.me> wrote:
Dann Corbit wrote:
What is the exact problem that you are trying to solve?


Well, I've now worked around the problem (still without knowing
how to build an array of strings). But it was part of a sudoku
solver. I was incorporating someone else's program for solving
exact cover problems and had to remap some data.


(Overly long lines corrected)

What exact problem involving the C language are you having, what
code are you having trouble with, what is it doing, and what
/should/ it be doing?

Since in C, strings are null-terminated arrays of char, an array
of strings could be implemented as an array of arrays of char.

char stringArr[5][10]; /* Allows 5 strings of max 9 length */

I'm not sure if the 5 and the 10 need to be switched for my
comment to be true.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 24 '06 #14

P: n/a

"Ben" <be*********@spam.me> wrote
Dann Corbit wrote:
What is the exact problem that you are trying to solve?


Well, I've now worked around the problem (still without knowing how to
build an array of strings). But it was part of a sudoku solver. I was
incorporating someone else's program for solving exact cover problems >
and had to remap some data.

Not all character data has to be in ASCIIZ strings.

Sudoku, in case anyone doesn't know consists of a 9x9 grid, containing
digits 1-9. No digit can be repeated in any line, column, or 3x3 box.

The obvious way to represent the board in C is

char sudoku[9][9].

Now say we want to print out the board to the user. The obvious way to do
that is

for(i=0;i<9;i++)
{
for(ii=0;ii<9;ii++)
putc(sudoku[i][i]]);
putc('\n');
}
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Jun 24 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.