473,782 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strings, Strings and Damned Strings

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
14 1448

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(mystrAr ray[0], <extra args> )
sprintf(mystrAr ray[1], <extra args> )
sprintf(mystrAr ray[2], <extra args> )

Jun 22 '06 #2
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
"Ben" <be*********@sp am.me> wrote in message
news:12******** *****@corp.supe rnews.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
"Dann Corbit" <dc*****@connx. com> wrote in message
news:e7******** **@nntp.aioe.or g...

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(mystrAr ray[i], "%02d", i);

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

return 0;
}
Jun 22 '06 #5
Ben
Dann Corbit wrote:
"Dann Corbit" <dc*****@connx. com> wrote in message
news:e7******** **@nntp.aioe.or g...

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(mystrAr ray[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,rown um;
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",poin ter->block);

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

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

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

strcat(alabel,r owid); strcat(alabel,c olid);
labelArray[i] = alabel;

strcat(blabel,d igit); strcat(blabel,r owid);
labelArray[i+n4] = blabel;

strcat(clabel,d igit); strcat(clabel,c olid);
labelArray[i+n4+n4] = clabel;

strcat(dlabel,d igit); strcat(dlabel,b lkid);
labelArray[i+n4+n4+n4] = dlabel;

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

pointer = pointer->next_in_puzzle ;
}

2) printf("\nConte nts of labelArray:\n") ;
for (j=0;j<4*n4;j++ ) {
printf("%s ",labelArra y[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

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
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
In article <12************ *@corp.supernew s.com>,
Ben <be*********@sp am.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
Ben <be*********@sp am.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
5778
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT" >>>c = extract(a,b) >>>print c "abcdefghijklmnop"
17
7400
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently interchangeable in all circumstances (as long as they're paired) so there's no overloading to muddy the language. Of course there could be some interesting problems with current code that doesn't make a distinction, but it would be dead easy to fix...
16
2436
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold any arbitrary data, even binary data such as photos and movies.They are of course also good for their traditional role of storing and manipulating text." This view of strings is about a decade out of date with modern programmimg practice. From...
383
12257
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a poor man's DBMS, a broken email server and various other /application/ servers to try and crack the Internet and IS markets. In the case where they didn't spend their own money to get companies to
4
10538
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three sets of character strings from the user. Then I want to run through each element and compare the two strings. If they match I print they match... I'm having a bit of trouble with the actual loop through each array using the pointers and comparing...
2
22608
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the amount and size of strings in the array won't be known until run time (in the example I have it in...
95
5427
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ____________________________________ | | | ------------------ | | | BUTTON | | | ...
10
2092
by: Krustov | last post by:
$rambo="daffy duck"; $rambo="mad max"; $rambo="daffy duck"; $rambo="superman"; etc etc How do i remove duplicate strings from a array ? . ('daffy duck' could appear more than twice in the array)
26
3054
by: machineghost | last post by:
First off, let me just say that as someone with no DBA training whatsoever, any help I can get with this issue will be very, very much appreciated. My company recently migrated our database from DB2 v7 to DB2 v9. We hired a consultant to help us, and things went pretty smoothly ... up until a few weeks after, when a co-worker tried to insert JavaScript in to our database. That's when we learned that v9, unlike v7, has a problem with...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.