473,507 Members | 8,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enumeration of Array Indicies?

Hey,

I'd like to make an array with one index for each letter of the
alphabet. OK, easy enough. But instead of referencing the letter J
with array[10], can I enumerate every letter with A being 1 so I could
instead reference J with array[J]?

I'm pretty new to C++ so have mercy :P

iw****@gmail.com

Sep 7 '05 #1
10 6726
certainly...

const char* letters[] = "ABC...Z";
enum LETTERS { A=0, B=1, C=2, ... };

then

letters[A];
<IW****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hey,

I'd like to make an array with one index for each letter of the
alphabet. OK, easy enough. But instead of referencing the letter J
with array[10], can I enumerate every letter with A being 1 so I could
instead reference J with array[J]?

I'm pretty new to C++ so have mercy :P

iw****@gmail.com

Sep 7 '05 #2

<IW****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hey,

I'd like to make an array with one index for each letter of the
alphabet. OK, easy enough. But instead of referencing the letter J
with array[10], can I enumerate every letter with A being 1 so I could
instead reference J with array[J]?

I'm pretty new to C++ so have mercy :P

iw****@gmail.com


You can (but I'm not sure why you'd want to).

You can define enumerations with specific values, or simply starting at a
specific values. But remember, the index of the first item in an array is
0, not 1. So, while you can do (for example):

enum
{
A = 1, B, C, D, E, F, G, H, I, J
};

....that leaves you with no way to index array[0] using your scheme. And if
you use all 26 letters of the alphabet, remember that you'll need to declare
"array" as an array of 27 items, not 26.

But your question makes me wonder, exactly what's in this array? You say
you want to "reference J with array[J]", but does that mean that there is a
character 'J' stored in array[J], or did you really mean you want to
"reference array[10] using array[J]"?

Also, you might want to look at using std::map, if what you want is a
mapping between characters (or strings) and data.

-Howard
Sep 7 '05 #3
IW****@gmail.com wrote:
I'd like to make an array with one index for each letter of the
alphabet. OK, easy enough. But instead of referencing the letter J
with array[10], can I enumerate every letter with A being 1 so I could
instead reference J with array[J]?


'J' needs to be a name if you want to use it as an index. As Martin
suggested, you can make a bunch of enumerators, from A to whatever, and
give them respective values. Or, you could have a bunch of contants,
like

const int A = 1, B = 2, ...

(and why do you want to make A equal to 1 and not 0?)

Or you could just write

array['J']

(given that you have enough room in that array).

V
Sep 7 '05 #4
It's to count the number of letters in a string. So I was just going
to go character by character, and it's an a, add one to letters[a], if
it's a b, add one to letters[b], etc. The array just stores integers

Sep 7 '05 #5
That didn't sound right. It's to count the number of occurances of all
the letters in a string.

TEST

letters[t] would = 2
letters[e] = 1
letterss] = 1

Sep 7 '05 #6
Dear IWP506,

On Wed, 2005-09-07 at 14:25 -0700, IW****@gmail.com wrote:
That didn't sound right. It's to count the number of occurances of all
the letters in a string.

TEST

letters[t] would = 2
letters[e] = 1
letterss] = 1


In that case I would opt for having a char as the index of the array.
Then you would write letters['t'] and so on. Reason: from a string you
are going to get chars and when the letters are indexed with a char no
further translation from char to enum is necessary, except for lowering
of the case of the letters of "TEST" as in you example, perhaps. Also if
you want to loop over character range and output the current character
no translation is necessary.

Best wishes,
Chris

Sep 7 '05 #7
IW****@gmail.com wrote:
That didn't sound right. It's to count the number of occurances of all
the letters in a string.

TEST

letters[t] would = 2
letters[e] = 1
letterss] = 1


Read about 'std::map' and use

std::map<char,unsigned>

to count only the ones that do happen.

V
Sep 7 '05 #8

<IW****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
It's to count the number of letters in a string. So I was just going
to go character by character, and it's an a, add one to letters[a], if
it's a b, add one to letters[b], etc. The array just stores integers


If that's the case, I think you've got a misconception regarding characters
in a string and their actual values. The letter 'a' (which differs from the
letter 'A', by the way), already has a value, and giving a value of 1 (or 0)
to the symbol a in an enumeration isn't going to help you do anything,
really.

Suppose you have an array of char containing the string "hello". What you
have, then, are the characters 'h', 'e', 'l', 'l', and 'o' (and possibly a
trailing value of zero, if the it's a c-style null-terminated string).
Let's just look at the first character, 'h'. Its value is determined by the
current character set, which in most cases will be the standard ASCII
character set, and for the letter 'h', that value is 104 (I think...I don't
have the chart handy). So which counter in your array do you want to
increment? Well, if you're only counting letters of the alphabet, then 'h'
is the 8th letter, so you want to increment array[7] (since the first
element is 0).

How do you do that easily, regardless of which letter you're encountering?
Well, if I recall correctly, the letters of the alphabet (at least the
English alphabet) are always given sequential values starting at whatever
'a' is (which is 97 in ASCII, I think). But it might not be 97 on some
system or with some character set. So you don't want to just subtract 97
from your character's value. The easiest way to determine which slot to
increment, then, without having to rely on 'a' being 97, is to subtract
whatever the value of 'a' actually is. But since a character's value can
just be specified by the character itself, you can simply increment the
item: array['h'-'a'].

So...suppose your counts are in an array called counts (wow, what
imagination! :-)), and the input string is in an array called inputstring.
Then, while looping through inputstring (assuming that you use i as your
loop variable), you can do:

counts[inputstring[i]-'a']++;

That gets the value at inputstring[i], subtracts 'a' from it (which is just
some integer value, most likely 97), and increments the item in counts at
that location. In the case of i=1, the letter there is 'h', so it
increments counts[7].

However!!! ...you'll have to do something different if you might have
capital letters, numbers, symbols, etc. I don't know what your requirements
are, whether you might see those, or what you want to do if you do see
those, so I'm not going to advise you on how to handle them (unless you ask
for specific help with those).

Hope this helps...

-Howard

Sep 7 '05 #9

"Howard" <al*****@hotmail.com> wrote in message
news:bc********************@bgtnsc05-news.ops.worldnet.att.net...

Never mind. Seeing your other post (which came while I was writing my last
response), this wasn't what you were looking for.

-Howard
Sep 7 '05 #10
Thanks for the replies

Sep 8 '05 #11

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

Similar topics

9
3425
by: Randell D. | last post by:
Folks, I can program fairly comfortably in PHP and can, for the most part using these skills and others that I've picked up over the years manage to read/understand most code in Javascript... so...
7
2114
by: abs | last post by:
ar = new Array() ar = {"key1":"value1", "key2":"value2, "key3":"value3"}; Anybody knows how to sort such arrray by values ? I've tried writing the comparing function and than...
47
5028
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
4
1666
by: Erik Foreman | last post by:
this is what I have 'variables defined as arrays Dim ceday(), ceti(), ceto(), ceproj(), cenotes() As String Dim cerow As Int32
16
2359
by: frizzle | last post by:
Hi there! I'd like to create a function which input is the result of a mySQL query. The output should be exactly the same, only not a mySQL result array, but a 'real' array. So it should also...
4
9576
by: chy1013m1 | last post by:
In Java, one can do the following: int array = {{1,2,1}, {1}, {0,0,0,0,0,0,0,-1}}; How can one declare such array in C++/C without using various new operators individually to each row indicies?...
45
4763
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
2
3810
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
how do I code generic functions to return the next item in an enumeration a) sorted by name, b) sorted by value c) sorted by declaration in a round-robin style ? for example the enum is Enum...
1
2392
by: Gilles Ganault | last post by:
Hello Out of curiosity, is there a better way in Python to iterate through an array, and return the index of each item that contains the bit somewhere in its value, ie. index() doesn't work...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7372
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...
1
7030
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...
0
7482
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...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.