0

I figured out how I can translate a userinput into Morse code. It works. The only thing messing with me is that no matter what input I give, at the end of the outcome it says (null) and I do not know what I have to change in my code to get rid of it. I thought it might be the end of the array wHich can not be translated

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* tableSetup();
char* all_Cap(char sentence[]);
char* trans_to_morse(char* morse[], int b);

int main()
{
    char* morse[1024];
    char sentence[256];


    fgets(sentence,256,stdin);
    all_Cap(sentence);

    int b=strlen(sentence);
    for(int i=0;i<b;i++){
            morse[i]=tableSetup(sentence[i]);
    }

    trans_to_morse(morse, b);

    return (0);
}

char* tableSetup(int i){
    char* table[256]={0};

    table['0']="-----";
    table['1']=".----";
    table['2']="..---";
    table['3']="...--";
    table['4']="....-";
    table['5']=".....";
    table['6']="-....";
    table['7']="--...";
    table['8']="---..";
    table['9']="----.";
    table['A']=".-";
    table['B']="-...";
    table['C']="-.-.";
    table['D']="-..";
    table['E']=".";
    table['F']="..-.";
    table['G']="--.";
    table['H']="....";
    table['I']="..";
    table['J']=".---";
    table['K']="-.-";
    table['L']=".-..";
    table['M']="--";
    table['N']="-.";
    table['O']="---";
    table['P']=".--.";
    table['Q']="--.-";
    table['R']=".-.";
    table['S']="...";
    table['T']="-";
    table['U']="..-";
    table['V']="...-";
    table['W']=".--";
    table['X']="-..-";
    table['Y']="-.--";
    table['Z']="--..";
    table['.']=".-.-.-";
    table[',']="--..--";
    table[':']="---...";
    table[';']="-.-.-.";
    table['?']="..--..";
    table['!']="-.-.--";
    table['-']="-....-";
    table['_']="..--.-";
    table['(']="-.--.";
    table[')']="-.--.-";
    table['"']=".-..-.";
    table['=']="-...-";
    table['+']=".-.-.";
    table['/']="-..-.";
    table['@']=".--.-.";
    table[' ']=".......";


    return(table[i]);
}

char* all_Cap(char sentence[]){
    int b=strlen(sentence);
    for(int i=0;i<b;i++){
        if(sentence[i]>=97 && sentence[i]<=122) sentence[i] -=32;
    }
    return(sentence);
}

char* trans_to_morse(char* morse[], int b){
    for(int i=0;i<b;i++){
        printf("%s ",morse[i]);
    }
    return(0);
}

Outcome:

How are you?
.... --- .-- ....... .- .-. . ....... -.-- --- ..- ..--.. (null)
Process returned 0 (0x0)   execution time : 6.915 s
Press any key to continue.
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 4
    [man fgets](https://linux.die.net/man/3/fgets): "If a newline is read, it is stored into the buffer". Suggest you do basic debugging. Run your program in a debugger and check out the value of the last character and step thru the code as it processes that last character. – kaylum Aug 04 '20 at 12:10
  • 2
    You could check if `morse[i]` is non-null before printing it: `if (morse[i]) printf("%s ", morse[i]);`. – Ian Abbott Aug 04 '20 at 12:19
  • 2
    Curious, Nice use of `'A'` in `table['A']=".-";`, but then code reverts to `97` in `if(sentence[i]>=97 && sentence[i]<=122) sentence[i] -=32;`. Perhaps`if(sentence[i]>='a' && sentence[i]<='z') sentence[i] -= 'a' - 'A';` or better yet `tolower()`. – chux - Reinstate Monica Aug 04 '20 at 12:26
  • 3
    Why are you re-populating the table every single time the `tableSetup` function is run? That's extremely inefficient. Just create your table as a global `const char *table[] = ...` – Marco Bonelli Aug 04 '20 at 12:28
  • 3
    Aside: Bug: `tableSetup(sentence[i])` is a problem when `sentence[i] < 0`. Suggest `char* tableSetup(int i)` --> `char* tableSetup(unsigned char i)` to avoid negative indexes. – chux - Reinstate Monica Aug 04 '20 at 12:31
  • @MarcoBonelli my teacher tells me to avoid anything global to learn how to hand over variables to other functions – Wladislaw Kusnezow Aug 04 '20 at 12:43
  • 4
    Your teacher is right in the general case, but this is a really clear and standard exception to that silly rule. Global variables are not evil if they are used correctly and when needed. What would be nonsense is to have `n` global for example, but for a table that is being used over and over that's just the obvious way to go (and it's also *a lot* faster). – Marco Bonelli Aug 04 '20 at 12:46
  • @WladislawKusnezow that array never change, if you do not want to have it global declare it `static`into *tableSetup*, also it does not contain `char*`but `const char*` – bruno Aug 04 '20 at 12:46
  • Does this answer your question? [How to access a local variable from a different function using pointers?](https://stackoverflow.com/questions/4570366/how-to-access-a-local-variable-from-a-different-function-using-pointers) – Marco Bonelli Aug 04 '20 at 12:49
  • using `char* table[256]` you suppose the code of a character is from 0 up to 255, out of the fact a character can be signed as signaled by @chux-ReinstateMonica you cannot suppose that even managing `unsigned char`, use `UCHAR_MAX+1` or `SCHAR_MAX-SCHAR_MIN+1` – bruno Aug 04 '20 at 13:02
  • 1
    `if(sentence[i]>=97 && sentence[i]<=122) sentence[i] -=32;`? Did you mean `sentence[i] = toupper(sentence[i];`? – Andrew Henle Aug 04 '20 at 15:04
  • @AndrewHenle so basically yes, i am new to coding and my way `if(sentence[i]>=97 && sentence[i]<=122) sentence[i] -=32;` was the easiest way i had in mind. Thank for your advise, gonna use this instead of mine :D – Wladislaw Kusnezow Aug 05 '20 at 06:12

1 Answers1

1

Just include an initialization line that says

table[`\n`] = "";

The problem is that fgets(3) includes the final \n character, so you try to print it, and the table entry you have for the \n character is NULL as you assigned it in the declaration.

Another solution is to map table['\n'] = "\n"; so a newline is mapped into a string with just a \n, and you'll separate the output morse code into lines, as you do with the input.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31