Please help with strcmp!

Hello everyone, i was asked to find corresponding decoded letter from the lookup table and compare it with users input.

I was able to perform string comparing between user input (buf) and const char *encodedStr.. However when i want to compare const char decodedChar and buf i don;t seem to be able to get it right.

Error message : cannot convert 'String' to 'const char*' for argument '2' to 'int strcasecmp(const char*, const char*)'

Where did i go wrong...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// _____________________ Header Files _____________________

#include <ee108.h>
#include <ee108_switches.h>
#include <string.h>

// _________________________ Types ________________________

struct MorseCode {
  const char decodedChar; // this represents the decoded character
  const char *encodedStr; // the represents the morse encoding
};

typedef  struct MorseCode    MorseCode;

// ___________________ Function declaration ________________

const MorseCode * findMorseCode(const char * combination);
void printCombination (const MorseCode * pMorseData);

// _______________________ Constants _______________________

// lookup table to allow conversion from characters to morse codes
// and from morse codes to characters
const MorseCode MORSE_TABLE[] = {
  { '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', "--.." },
  { '0', ".----" },
  { '1', "..---" },
  { '2', "...--" },
  { '3', "....-" },
  { '4', "....." },
  { '5', "-...." },
  { '6', "--..." },
  { '7', "---.." },
  { '8', "----." },
  { '9', "-----" }
};

const int SUPERLOOP_DELAY_MS = 25;

const int MORSE_TABLE_SIZE = sizeof(MORSE_TABLE) / sizeof(MORSE_TABLE[0]);
const int BUFFER_SIZE = 11;

// ___________________ Function Declarration __________________

int compare_strings(char a[], char b[]);

// ____________________________________________________________

void setup() {

  // set up serial
  Serial.begin(9600);
  Serial.println("\n\n\t________A4_SerialMorseToChar________\n");

  Serial.println("\n\tType a pattern of . and - chars & hit send:");

}

void loop() {

  char buf[BUFFER_SIZE] = "";

  int bufIndex = 0;

  // read characters & break out of while loop if end of line,
  // space character or max buffer size is detected.
  while (bufIndex < BUFFER_SIZE) {
    char symbol;

    // wait until at least one character has been entered
    while (Serial.available() == 0) {
      delay(10);
      continue;

    }
    // OK, we now have at least 1 character
    symbol = Serial.read(); // read 1 character

    // a newline or space indicates the end of the word
    // so we'll break out to process it
    if ((symbol == '\n') || (symbol == '\r') || (symbol == isspace(symbol))) {
      if (bufIndex > 0)

        break;
    }

    // ignore any characters that are not......
    if (symbol != '.' && symbol != '-' && !isalpha(symbol) && !isdigit(symbol))
      continue; // go directly to next iteration of while loop

    // at this point only valid characters should be left
    buf[bufIndex] = symbol; // add the character to the buffer
    bufIndex++; // increment index

  }

  // newline, space or max number of characters?
  buf[bufIndex] = '\0'; // add nul terminator to buf

  Serial.print("\tInput was: ");
  Serial.println(buf);

  // ...........................................................
  String str(MORSE_TABLE[0].decodedChar);
  // I will add null terminator in more appropiate way later..
  str[1] = '\0';

  Serial.println(str);
  //Serial.println(strcasecmp(buf, str));
  Serial.println(MORSE_TABLE[0].decodedChar);

  Serial.println(strcasecmp(buf, str));

  // ...........................................................

  for (int i = 0; i < strlen(buf); i++) {

    if (buf[i] != '.' && buf[i] != '-') {
      Serial.print("\tDecoded character is: ");
      Serial.println("LETTER");
      break;
    }

    else {
      int match = findMorseCode(buf);
      printCombination(match);
      break;
    }
  }

  if (Serial.available() == 0)
    Serial.println("\n\tType a pattern of . and - chars & hit send:");
}



// ___________________ Function Declarration __________________

// _______________________ Function 1 _______________________
const MorseCode * findMorseCode(const char * combination) {
  int i;

  for (i = 0; i < MORSE_TABLE_SIZE; i++) {

    if (strcasecmp(combination, MORSE_TABLE[i].encodedStr) == 0)
      return &MORSE_TABLE[i]; // yes, return a pointer to the matching entry
  }
  return NULL;
}

// _______________________ Function 2 _______________________

void printCombination (const MorseCode * pMorseData) {

  Serial.print("\tDecoded character is: ");

  if (pMorseData)
    Serial.println(pMorseData->decodedChar);

  else
    Serial.println("UNKNOWN");
}

// ____________________________________________________________

Anyone, please!
So what is String?
closed account (48T7M4Gy)
I see you have decided to take the advice I gave and use String's.

You have a number of options but the easiest way to solve the difficulties is the use String type throughout and not use c-style strings.

You should look at how the data is sent and received. My bet is it is sent as a string of data, not individual dots and dashes interspersed with other stuff not relevant to the encoded message. Therefore you don't have to load a buffer, simply read a String. Strings are tested for equality by == not strcmp.

So if you receive a String str = "--..." then when you search the table
if (str == "--...") then character = '6'

This is contained in the reference material I quoted earlier:
https://www.arduino.cc/en/Reference/StringComparison
Topic archived. No new replies allowed.