Enumeration of Array Indicies?

I

IWP506

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

(e-mail address removed)
 
M

Martin Vorbrodt

certainly...

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

then

letters[A];
 
H

Howard

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

(e-mail address removed)

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
 
V

Victor Bazarov

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
 
I

IWP506

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, etc. The array just stores integers
 
I

IWP506

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
 
C

Chris Dams

Dear IWP506,

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
 
V

Victor Bazarov

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
 
H

Howard

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, 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-'a']++;

That gets the value at inputstring, 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
 
H

Howard

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

-Howard
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top