An encoded file will be decoded by the program using a key input from the user. This key is a sequence of numbers that will scramble the message in the file into its appropriate order.

I have to use linked lists for this. I already started it, but I can't seem to find a correct way to jumble the words and display them correctly.

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

typedef struct _node_{
char word[30];
struct _node_ *pPtr_word;
}node_t;

int main(void)
{
FILE *pInput, *pOutput;
int a, b, count, num, group_num, counter, data[10], i, j;
node_t node[100];
node_t *pPtr;

pInput=fopen("input.txt","r");
if(pInput==NULL){
printf("Error: can't open file.\n");
return 1;
}
else{
if('\n')
count=count++;
for(a=0;a<count+1;a++)
fgets(node[a].word,30,pInput);
for(b=0;b<count+1;b++)
printf("%s\n", node[b].word);
}

printf("Enter number of numbers for key sequence: ");
scanf("%d", &num);
printf("Enter key sequence: ");
for(counter=0;counter<num;counter++)
scanf("%d", &data[counter]);

group_num=((count+1)/num);

for(i=0;i<group_num;i++)
for(j=0,pPtr=node;j<num-1;j++,pPtr++)
node[(data[j]-1)+num*i].pPtr_word=&node[...
pPtr->pPtr_word=NULL;

for(pPtr->word;pPtr!=NULL;pPtr=pPtr->pPt...
printf("%s", pPtr->word);

getchar();
getchar();

return 0;
}

Example, the file input [from a text file] is-- brown quick The over jumped fox dog. lazy the
Then the key sequence entered would be 321
The output should be-- The quick brown fox jumped over the lazy dog.

In my text file, it looks like this:
brown
quick
The
over
jumped
fox
dog.
lazy
the

Also, for displaying my linked list, it seems like it doesn't traverse the whole thing. I also can't seem to create the right formula to get the correct sequence [since there can be 2 or 4 numbers for the sequence and it should work for all cases].

Please help me out :)

> if('\n')
What's this supposed to do?

> node[(data[j]-1)+num*i].pPtr_word=&node[...
> pPtr->pPtr_word=NULL;
This won't even compile.

> count=count++;
Just count++;
But since count seems uninitialised at this point, who knows.

The code is missing half the { } it needs to make it make sense.

And the total lack of indentation makes the rest of it impossible to follow.

On the plus side, you did seem to manage to use code tags on the first attempt, which is a rare achievement in itself.

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

typedef struct _node_{
     char word[30];
     struct _node_ *pPtr_word;
     }node_t;
     
int main(void)
{
FILE *pInput, *pOutput;
int a, b, count, num, group_num, counter, data[10], i, j;
node_t node[100];
node_t *pPtr;
pPtr=malloc(sizeof(node_t));

     pInput=fopen("input.txt","r");
     if(pInput==NULL){
          printf("Error: can't open file.\n");
          return 1;
     }
     else{
          if('\n')
               count=count++;
               for(a=0;a<count+1;a++)
                    fgets(node[a].word,30,pInput);
               for(b=0;b<count+1;b++)
                    printf("%s\n", node[b].word);
     }

     printf("Enter number of numbers for key sequence: ");
     scanf("%d", &num);
     printf("Enter key sequence: ");     
     for(counter=0;counter<num;counter++)
          scanf("%d", &data[counter]);

     group_num=((count+1)/num);

     for(i=0;i<group_num;i++){
          for(j=0,pPtr=node;j<num-1;j++,pPtr++)
          node[((data[j])+num*i)-1].pPtr_word=&node[(data[j])+num*i];
     }
     pPtr->pPtr_word=NULL;

     for(pPtr->word;pPtr!=NULL;pPtr=pPtr->pPtr_word)
          printf("%s", pPtr->word);

     getchar();
     getchar();

     return 0; 
}

I re-pasted it [so that there will be indentations]

I am getting input from a file, and I am getting it per line so there is that if('\n').

The count is for word count purposes... initializing it to 0 doesn't seem to help.

This part: node[((data[j])+num*i)-1].pPtr_word=&node[(data[j])+num*i]; is the supposedly "formula" to get the correct sequence.. I haven't figured out the correct formula.

But my biggest problem is displaying it. Even if I don't get the sequence correctly, at least I should be able to output the sentence, not just a word.

This article has been dead for over six months. Start a new discussion instead.