Array lookup as a string AND datatype problems.

What I want to do is receive a letter by:

Serial.println("insert first letter here");
    while(Serial.available() == 0) { }  
      char hIpt8 =;

This pretty much stops the code from running further until it receives input, that input is then assigned to the variable, hIpt8. I want to look up that variable in and array such as this:

String letterArray[] = 
      "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", 
      "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"

I want it to return the array value, meaning when you get array values like so:


I want to find the X value of the location of my variable (hIpt8) inside the array. For example, I type "a" into the Serial input, and it will return the location of that letter in the array, in this case "1".


Why not just subtract 'a' from your incoming char value?

'a' - 'a' = 0 'b' - 'a' = 1 'c' - 'a' = 2... etc.

Basically, I want to do an extremely basic 4-bit encryption so it sends over a random scramble of letters associated with an EEPROM lookup key and the result is in HEX. It needs to first de-HEX it, read the key, retrieve the key from the EEPROM, and de-encrypt it by looking up the key number for each bit in the letterArray.

Ok. So first, do NOT use String just for one single character, use the char data type:

char *letters = "abcdefghijklmnopqrstuvwxyz";

Then, you can use “index” on it to find the character:

char mychar = 'f';

int idx = index(letters, mychar);

// idx == 5
INDEX(3)                                                                      Linux Programmer's Manual                                                                     INDEX(3)

       index, rindex - locate character in string

       #include <strings.h>

       char *index(const char *s, int c);

       char *rindex(const char *s, int c);

       The index() function returns a pointer to the first occurrence of the character c in the string s.

       The rindex() function returns a pointer to the last occurrence of the character c in the string s.

       The terminating null byte ('\0') is considered to be a part of the strings.

       The index() and rindex() functions return a pointer to the matched character or NULL if the character is not found.

       4.3BSD; marked as LEGACY in POSIX.1-2001.  POSIX.1-2008 removes the specifications of index() and rindex(), recommending strchr(3) and strrchr(3) instead.

       memchr(3), strchr(3), string(3), strpbrk(3), strrchr(3), strsep(3), strspn(3), strstr(3), strtok(3)

Wonderful, thank you! I need to download an "index" library in order to use the function correct?

No. index() is (or should be) part of the standard internals of C.

The Arduino IDE is not recognizing it. It returns the error:

hasher_v1_5:54: error: 'index' was not declared in this scope

Then maybe they are lacking that function then. hmmm.. We'll have to do it with strchr then.

strchr works similarly to index, but it returns a pointer to the character in the string, not the offset of the character in the string, so we'd need to dereference it:

char mychar = 'f';
char *letters = "abcdefghijklmnopqrstuvwxyz";

char *ptr = strchr(letters, mychar);
if (ptr != NULL) {
    int idx = ptr - letters;
    // idx == 5
} else {
  // Not found in the string

Wonderful, that was greatly appreciated!