Getting the index of a string array.

I have an array that looks like this:

char* myArray[150]={"IY","IH","FY","EH","AY","AX","UX","OH","AW","OW","UH","UW","MM","NE"};

and I want to get the index number of say "AX".

I can type:


and it returns AX, however I want it through only knowing the letters!

how can I do this?

p.s. hello, this is my first post. I am relatively new to arduino but I like it.

Hello and welcome

The only way is to use a for loop

I mean something like this:

int getIndexByKey( const char * key )
	for ( uint8_t i = 0; i < sizeof( myArray ) / sizeof( char * ); i++ )
		if ( !strcmp( key, myArray[i] ) )
			return i;
	return -1;


Serial.println( getIndexByKey( "AX" ) );

If you explain exactly what you are trying to do, there may be a much more elegant or more efficient solution :wink:

Also you shouldn’t size an array bigger than what you actually put in it. So instead of counting yourself or put a “max” value like you did, just don’t put any size, the compiler is smart enough to count. Change




Of course this is valid only if your array never change, in which case you should also use the const keyword.

Thank-you very much, I am going to try your solution. I will let you know how it goes..

That’s what i’ve put… It says ‘key’ was not declared in the scope.

void setup() {
  Serial.println("Begin: ");

void loop() {
char* myArray[]={"IY","IH","FY","EH","AY","AX","UX","OH","AW","OW","UH","UW","MM","NE","NO","NGE","LE","LO","WW","RR","IYRR","EYRR","AXRR","AWRR","OWRR","EYIY","OHIY","OWIY","OHIH","IYEH","EHLL","IYUW","AXUW","IHWW","AYWW","OWWW","JH","VV","ZZ","ZH","DH","BE","BO","EB","OB","DE","DO","ED","OD","GE","GP","EG","OG","CH","HE","HO","WH","FF","SE","SO","SH","TH","TT","TU","TS","KE","KO","EK","OK","PE","PO"};
  int getIndexByKey( const char * key );

	for ( uint8_t i = 0; i < sizeof( myArray ) / sizeof( char * ); i++ ) {
		if ( !strcmp( key, myArray[i] ) ) {
			return i;
	return -1;

  Serial.println( getIndexByKey( "AX" ) );

If you leave a NULL at the end of the array, then you don’t need to keep count at all.

char* myArray[]={"IY","IH","FY","EH","AY","AX","UX","OH","AW","OW","UH","UW","MM","NE",NULL};

int find(char* s) {
  for(int i = 0; myArray[i]; i++) {
    if(!strcmp(s, myArray[i])) return i;

  return -1; // -1 means "not found"

Paul Murray it states "A function-definition is not allowed before a "{'

referring to the line: int find(char* s) {

So unsure what to do here..

You got a typo somewhere before that function. Account your brackets.

I copied it verbatim, tried a few different things.. still stuck. It's probably starring me in the face, thanks mistergreen for your input.

I'm not sure what the little 's' represents, sorry It's been a while so I'm not confident with all the syntax yet.

OK, that looks like a simple misunderstanding on the syntax. The function which guix wrote for you belongs OUTSIDE any other function. loop() and setup() are just functions. Your code in reply #3 tried to put this function inside the loop() function and didn't correctly copy-paste the code that guix wrote.

When we say 'inside' we mean inside the curly-braces {} which come after the function name.

Go back and copy-paste the function into your code, somewhere outside loop() and setup() and then 'call' the function (ask the function to return its result) from inside the loop() function, as demonstrated in the example right at the bottom of guix' first code block.

aaagh, function outside the loop and setup, calling the function from inside. I get it. I will give this a go.

hope it works

Morgan, your pointers were exactly what I needed, thank-you!

This is officially solved, still got more to do, but you made this one little road-block cleared for me BIG THANKS.