I have strings like "− · · · −" (Morse code) in an array, and want to tokenize each string to get each individual dot(.) and dash(−). A part of my code is given below:
char *code, *token;
char x;
char ch[4096];
code = &ch[0];
..
while((x = tolower(fgetc(fp))) != EOF){
printf("%c \n", x);
switch(x){
case 'a':
strcpy(code, "· −");
break;
case 'b':
strcpy(code, "− · · ·");
break;
case 'c':
strcpy(code, "− · − · ");
break;
case 'd':
strcpy(code, "− · ·");
break;
case 'e':
strcpy(code, "· ");
break;
case 'f':
strcpy(code, "· · − ·" );
break;
case 'g':
strcpy(code, "− − · ");
break;
case 'h':
}
if(x!= 10){
printf("Value read : %s \n", code);
token = strtok(code, " ");
while(token != NULL){
printf("CHARACTER: %s\n", token);
token = strtok(NULL, " ");
}
}
So, when the code array has "− − ·", I want the output to have:
CHARACTER: −
CHARACTER: −
CHARACTER: ·
However, the output is instead having CHARACTER: − − · I am new to string tokenizing, and might made a mistake somewhere there. Perhaps my delimiter is wrong, I am not sure. I hope I have provided enough information. Any help on this would be greatly appreciated.
Thanks in advance