468,272 Members | 2,045 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

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 6402
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Randell D. | last post: by
7 posts views Thread by abs | last post: by
4 posts views Thread by Erik Foreman | last post: by
16 posts views Thread by frizzle | last post: by
4 posts views Thread by chy1013m1 | last post: by
2 posts views Thread by =?Utf-8?B?aGVyYmVydA==?= | last post: by
1 post views Thread by Gilles Ganault | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.