f



Strings, Strings and Damned Strings

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
0
6/22/2006 2:28:05 AM
comp.lang.c 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

14 Replies
1340 Views

Similar Articles

[PageSpeed] 40

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

0
pythagras (11)
6/22/2006 3:09:06 AM
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.

0
santosh.k83 (3969)
6/22/2006 3:11:51 AM
"Ben" <ben.carbery@spam.me> wrote in message 
news:129k01n8e6l6328@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 


0
dcorbit (2697)
6/22/2006 3:13:25 AM
"Dann Corbit" <dcorbit@connx.com> wrote in message 
news:e7d1kr$tl1$1@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;
}


0
dcorbit (2697)
6/22/2006 3:22:37 AM
Dann Corbit wrote:
> "Dann Corbit" <dcorbit@connx.com> wrote in message 
> news:e7d1kr$tl1$1@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.

0
6/22/2006 5:10:00 AM
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.

0
bill.pursell (771)
6/22/2006 6:07:21 AM
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.
0
6/22/2006 6:27:14 AM
In article <129ke23qri3f458@corp.supernews.com>,
Ben  <ben.carbery@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

0
gazelle2 (1307)
6/22/2006 12:08:38 PM
Ben <ben.carbery@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.
0
blp (3955)
6/22/2006 5:43:20 PM
In article <87ejxh6q87.fsf@benpfaff.org>,
Ben Pfaff  <blp@cs.stanford.edu> wrote:
>Ben <ben.carbery@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
0
roberson2 (8602)
6/22/2006 6:12:07 PM
"Ben" <ben.carbery@spam.me> wrote in message 
news:129k9haqlmv7i68@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?


0
dcorbit (2697)
6/24/2006 12:09:45 AM
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.
0
6/24/2006 1:44:20 AM
On 2006-06-24, Ben <ben.carbery@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.
0
apoelstra (387)
6/24/2006 3:20:25 AM
"Ben" <ben.carbery@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


0
regniztar (3128)
6/24/2006 5:09:33 AM
Reply: