Hello, I am writing a program that takes a sentence and tranforms that sentence into the morse code equivalent. Problem is, when I print the text after the convertion, all the letters after the 1st letter are the same morse code symbol, instead of the correct letter equivalent :/ I been looking over my code trying to see why this is happening, but I cannot see what the problem can be! Can anyone please help me see what I am doing wrong? I'd surely appreciate any help!
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LETTERS 26
#define NUMBERS 10
void convertToLower(char * sPtr);
int main(int argc, const char * argv[])
{
// Morse Code 0-9
//char * num_codes[] = {"-----", "----.", "---..", "--...", "-....", ".....", "....-", "...--", "..---", ".----"};
// Morse Code A-Z
char * alp_codes[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
// A-Z
const char searchKey[] = {'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'};
// string variables & attributes
char random_text[] = "abcd";
char * txtPtr = random_text;
int text_size = sizeof(random_text)/sizeof(random_text[0]);
char * morse_code[text_size];
int alp_index;
convertToLower(random_text);
// seaches for the searchKey in the txt and
// converts that character of random_txt to the alp_code
for(int h = 0; h < text_size; h++)
{
for(int i = 0; i < LETTERS; i++)
{
txtPtr = strchr(txtPtr, searchKey[i]);
alp_index = (int)(txtPtr - random_text);
if(txtPtr != NULL)
{
txtPtr++;
morse_code[h] = alp_codes[alp_index];
break;
}
txtPtr = random_text;
}
}
// prints morse code
for(int i = 0; i < text_size-1; i++)
{
printf("%s | ", morse_code[i]);
}
return 0;
}
//----------------------------------------------------------------------------------------
// FUNCTION DEFINITIONS
void convertToLower(char *sPtr)
{
while(*sPtr != '\0')
{
*sPtr = tolower(*sPtr);
*sPtr++;
}
}