1

I have a function in my program that is supposed to take Morse code input, compare it to an array of strings and return a letter from a corresponding string once it has found the matching Morse. I've finally managed to get it to run without crashing, but now it keeps returning the wrong letters. For instance ... --- ... should return sos but instead I get amb. I tried testing it by printing out the index number, the morse code string and the letter and it all matched up, so I think the problem is with string compare.

Here's the code:

void morsetotext(char mor[])
{
     char alpha[]={"abcdefghijklmnopqrstuvwxyz1234567890 "};
     char *morse[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", 
     "..", ".---","-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", 
     "...", "-", "..-", "...-",".--", "-..-", "-.--", "--.","-----", ".----", 
     "..---", "...--", "....-",".....", "-....", "--...", "---..", "----." "/ "};
     char letter[8];
     char convert[250];
     int con_count=0;
     int let_count=0;
     int count=0;
     int index=0;
     int length=strlen(mor);

     while (count<length)
     {
           for(let_count=0; let_count<8 && mor[count]!=' '; let_count++)
           {
                            letter[let_count]=mor[count];
                            count++;
           }

           letter[let_count+1]='\0';

           index=0;
           while (strcmp (letter, morse[index])!=1)
           {
                 index++;
           }

           count++;

           printf ("%c", alpha[index]);
     } 
     return;
}

Thanks for any help.

Edit: Sorry about that, here's the whole function.

3
  • Show us the declaration and definition of letter. Sounds like it may be a char, in which case your call to strcmp is wrong. Do you have your warning level turned up? Are you ignoring any warnings? Commented Dec 3, 2012 at 21:01
  • Yes, the problem is clearly with string compare... a function that hundreds of thousand of programmers have used millions of times without a problem. Read "The first rule of programming": codinghorror.com/blog/2008/03/… Commented Dec 3, 2012 at 21:07
  • I meant that I thought I might be using it wrong. I'm still learning, and I was hoping for more experienced eyes to help me find the problem. Thank you though, for your polite response. Commented Dec 3, 2012 at 21:17

3 Answers 3

5
while (strcmp (letter, morse[index])!=1)

You probably meant 0 instead of 1. Or just say while (!strcmp(...)).

Sign up to request clarification or add additional context in comments.

Comments

5

Compare strcmp() against 0, not 1. the function will only return 0 with a full match. Read the manual! :)

3 Comments

If you mean change it to while (strcmp (letter, morse[index]) !=0), I've tried that, repeatedly, and it crashes the program.
@user1827962: A valid comparison will never crash a program, so either letter is the wrong type or your incrementing count past the end of morse or alpha. Please answer the question I asked in the comment. Either way, comparing against 1 is wrong, and I can guarantee you that strcmp is not broken.
The program will also crashes if you feed it a morse code it can't recognize because 'index' will run beyond the end of the morse[] array.
1

This statement:

letter[let_count+1]='\0';

is writing to letter[9] if the input (mor) is 8 characters long.

You declared letter as char letter[8];, so the only valid indicies are [0] - [7].

Assigning to letter[9] is most likely causing the seg-fault you describe.

It seems to me that you want letter to contain up to 8 data characters, plus one null-terminator (\0). That suggests you should declare it as char letter[9];.

1 Comment

This was the source of the seg-fault. Thank you for pointing it out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.