Strings, Strings and Damned Strings

B

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
 
D

doug turnbull

Ben said:
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> )
 
S

santosh

Ben said:
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.
 
D

Dann Corbit

Ben said:
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.
 
D

Dann Corbit

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, "%02d", i);

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

return 0;
}
 
B

Ben

Dann said:
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, "%02d", i);

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

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 = 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.
 
B

Ben

Bill said:
Ben said:
char *labelArray [(4*MAX2*MAX2)+1];

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.
 
K

Kenny McCormack

B

Ben Pfaff

Ben said:
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';
 
W

Walter Roberson

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...
 
D

Dann Corbit

[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?
 
B

Ben

Dann said:
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.
 
A

Andrew Poelstra

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.
 
M

Malcolm

Ben said:
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]);
putc('\n');
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top