0

I am trying to take a user inputted string and look at each code to see if it appears in another string of strings. So far my code works. If the word is successfully found then the alpha representation is to be added to an array that will eventually be printed, but only if all codes were found.

I am having issues with what gets stored in my array that is going to be printed.

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

typedef char *string;
typedef char *alpha;

int main(void)  
{
   string morse[4]={".-", "-...","----.", ".."};
   string alpha[4]={"A",  "B",   "9",     "I"};
   char prntArr[50];
   char *input;
   char *hold;
   input = malloc(200);
   hold = malloc(50);
   int i=0;
   int j=0;
   int ret;
   int x;
   int w=0;
   int z=0;
   printf("please enter a string\n");
   scanf("%[^\n]",input);

   do{
      if (input[i] !=' ')
      {
         hold[j] = input[i];
         j++;
      }
      else
      {
         hold[j]='\0';                

         for (x=0;x<4;x++)
         {
            printf("value of x  %d\n",x);

            ret = strcmp(morse[x], hold);
            if (ret==0)
            {
               printf("%s\n",alpha[x]);

               prntArr[w]=*hold;
               w++; 
               x=4;
            }
            else
            {
               ret=1;
               printf("invalid Morse code!");

            }
         }
         j = 0;
      }
      i++;
   }while(input[i] !='\0');

   for (z=0;z<50;z++)
   {
      printf("%c",prntArr[z]);
   }

   return 0;
   free(input);       
}
1
  • Please add sample input and expected output. Commented Sep 25, 2016 at 3:02

1 Answer 1

1

The problem you asked about is caused by the way prntArr is used in the program. It really should be an array of character pointers into the alpha array. Instead, it's manipulated as an array of characters into which the first character of each morse code element is stored. And when it's printed, the variable that tracks how much of the array is used is simply ignored.

Another problem is that your code uses spaces to break the codes but there won't necessarily be a space at the end of the line so a code might get missed. In the program below, I switched out scanf() for fgets() which leaves a newline character on the end of the input which we can use, like space, to indicate the end of a code.

Other problems: you print the invalid Morse code message at the wrong point in the code and you print it to stdout instead of stderr; you remember to free input but forget to free hold; you put code after return that never gets called.

Below is a rework of your code that addresses the above problems along with some style issues:

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

int main(void)
{
    char *morse[] = {".-", "-...", "----.", ".."};
    char *alpha[] = {"A" , "B"   , "9"    , "I" };

    char *print_array[50];
    int print_array_index = 0;

    char hold[50];
    int hold_index = 0;

    char input[200];
    int i = 0;

    printf("please enter a string: ");
    fgets(input, sizeof(input), stdin);

    while (input[i] !='\0') {

        if (input[i] ==' ' || input[i] == '\n')
        {
            hold[hold_index] = '\0';

            bool found = false;

            for (int x = 0; x < sizeof(morse) / sizeof(char *); x++)
            {
                if (strcmp(morse[x], hold) == 0)
                {
                    print_array[print_array_index++] = alpha[x];

                    found = true;

                    break;
                }
            }

            if (!found)
            {
                fprintf(stderr, "invalid Morse code: %s\n", hold);
            }

            hold_index = 0;
        }
        else
        {
            hold[hold_index++] = input[i];
        }

        i++;
    }

    for (int x = 0; x < print_array_index; x++)
    {
        printf("%s ", print_array[x]);
    }

    printf("\n");

    return 0;
}

SAMPLE RUNS

> ./a.out
please enter a string: ----. -... .- ..
9 B A I 
> 

> ./a.out
please enter a string: .- --- ..
invalid Morse code: ---
A I 
>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help I really need to understand how pointers arrays and strings all work. I can start to see how they are.
I now need to go in reverse. I have added an if else statement that checks the first element to see if it is a do, dash or character. If it is a character then I need to compare it and print out the Morse code. I seem to be having problems using the strcmp. I think it may be because I am comparing alpha[x] with input[I]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.