Iterate through an array and return the position but from a different array

Hi,

So, I’m having a problem - I’ve figured out I can do this easily with a big case statement, but it is not efficient at all and I dislike it.

I have two arrays, both are the same size, one represents characters in one language and the second array, the same characters but in a different language.

I have tried iterating with a for loop the length of the arrays, but it did not work. Here was my attempt:

for (int i = 0; i < 26; i++) {
if (c == letters[i]) {
return morse[i];
}
}

This loop is inside a function, with its parameter set as C. The function receives inputs like ‘E’, or ‘O’. There aren’t any error codes however it also produces no output for me.

Thanks!

a) please make a small compileable prototype which shows your problem. This part of code doesn't show your specific error. Don't forget to post code in code tags

b) just in case if your variable letters would containt 'A', 'B', 'C', I guess it is not needed at all
as you can access morse[i] with a simple

i = c - 'A';

if c would be 'B', i becomes 1 -> the second value in your array therefore no need for array letters

You can't use strcmp() on char variables. It only works with strings. In C, strings are arrays of char with a null character (code 0) indicating the end of the string.

Your earlier code was better in this respect, but it was not complete, so we could not tell you what the problem was.

PS. Thanks for using code tags!

Thanks so much for helping! The solution now seems to work, I swear I tried it earlier but I must've been missing something that I later added.

Thanks, you guys are lifesavers :slight_smile:

no need for abc ...

const char * error = "err";

const char* morse[] =
{
  ".-",
  "-...",
  "-.-.",
  "-..",
  ".",
  "..-.",
  "--.",
  "....",
  "..",
  ".---",
  "-.-",
  ".-..",
  "--",
  "-.",
  "---",
  ".--.",
  "--.-",
  ".-.",
  "...",
  "-",
  "..-",
  "...-",
  ".--",
  "-..-",
  "-.--",
  "--..",
  " "
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  Serial.println(char2morse('E'));
  Serial.println(char2morse('T'));
  Serial.println(char2morse('S'));
  Serial.println(char2morse('O'));
  Serial.println(char2morse('$')); // unknown should through error
}

const char * char2morse(char c) {
  c = tolower(c);
  if (c >='a' && c <= 'z')
  {
    byte i = c - 'a';
    return morse[i];
  }
  else
    return error;
}

void loop() {
  // put your main code here, to run repeatedly:

}

and activate the processor warnings in your IDE!

You could be more helpful, when it comes to "saving the lives" of other beginners. For example, other beginners may be searching for help, find this topic and wonder why I told you not to use strcmp() and why I thanked your for using code tags? They will never see the post you deleted where you posted, using code tags, code using strcmp() so this topic will make no sense and confuse them even more. So, please never delete posts and only modify previous posts to do things like correcting spelling errors or adding code tags when they were forgotten. Don't delete or modify a topic in a way which means it no longer makes sense as a "story" for others reading it.