I'm sorry to say that there are many problems with this code. Let's start by getting charToDigit() working.
The name charToDigit() implies that the function takes a character and returns a digit. In truth, it does neither. It takes a Morse string representing a single character and returns the character (as a string). Let's rename it morseToChar(const char *). Since it returns just one character, let's make it
char morseToChar(const char *morse)
You can't compare strings with ==. Instead you'll have to write your own function. Since the standard library function is called strcmp(), let's call it StrCmp() to follow your naming convention in StrCat(). See the description of strcmp here:
http://www.cplusplus.com/reference/cstring/strcmp/
Write this function. Then test it with the following code which you should temporarily insert at the beginning of main()
1 2 3 4 5
|
cout << "hello vs. hello: " << StrCmp("hello", "hello") << '\n';
cout << "hello vs. hello there: " << StrCmp("hello", "hello there" << '\n';
cout << "empty string vs xyz: " << StrCmp("", "xyz") << '\n';
cout << "two empty strings: " << StrCmp("", "") << '\n';
return 0;
|
Once you're convinced that StrCmp works, change charToDigit() to morseToChar() like this:
1 2 3 4 5 6 7
|
char morseToChar(const char *morse)
{
if (StrCmp(morse, ".-") == 0)
return 'A'; // Note that this is the character 'A' and not the string "A"
else if (StrCmp(morse, "-...") == 0)
return 'B';
// etc;
|
When that's done, put some temporary code at the beginning of main() to test morseToChar(), similar to what you did for StrCmp.
I think you'll find it much easier to use array syntax (e.g, s1[i]) rather than pointer syntax (e.g., *(s1+i)).
To help out a little, I've written StrLen() and StrCpy(), and also implemented StrCat using StrLen and StrCpy. These are untested but I think they're correct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
unsigned StrLen(const char *s)
{
unsigned i;
for (i=0; s[i]; ++i)
;
return i;
}
char *StrCpy(char *dst, const char *src)
{
unsigned i=0;
do {
dst[i] = src[i];
} while (src[i++]);
return dst;
}
char *
StrCat(char *s1, const char *s2)
{
StrCpy(s1+StrLen(s1), s2);
return s1;
}
|