Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Strings Format in C #include <stdio.h> Decode Lab Your assignment is to write a

ID: 3710053 • Letter: S

Question

Strings Format in C
#include <stdio.h>

Decode Lab Your assignment is to write a C program, decode message. The message will be 26 characters in length, and will a as a series of numbers. These numbers must be sorted and compartoyo translation set of characters in order to decode it. This method is basenta decoder rings that used to be available in packages of cereal. e given mpared red against upon the show how a message is encoded. First, the letters of the alphabet, the numbers, an punctuation is scrambled into some sort of sequence. Perhaps the easiest way to show how to decode a message is to first s For example, the string below can be found in codefile.txt: Notice that the above string contains 52 characters (including the nine blanks near the end). Now, let's suppose that we want to encode the message: HELP ME! Looking up each of the letters in the above string we find that H is the 11th character (zero-based indexing), E is the 14th, L the 2nd, P the 5th, one of the blanks the 41st, M the 15th, E the 14th, and ! the 12th. Therefore, our initial coding is 11 14 02 05 41 15 14 12 Next, each number is assigned a three-digit sequence number, in ascending order, to precede it. These numbers indicate relative position and need no consecutive. t be

Explanation / Answer

As per your requirement the below one is solution please follow it

decode.c

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


void handle_line(char *line) {
printf("%s", line); //the line got the file
}

char* handle_line_picked(char *line) {
return line; //not completed but theres the structure
}
char* getCode(){
  
    int size = 53, pos; //53 chars taking in account the NULL at the end
    int c;
    char *buffer = (char *)malloc(size);

    FILE *f = fopen("codefile.txt", "r");
  
    if(f) {
      do { // read all lines in file (should be only one, the code...)
        pos = 0;
        do{ // read one line
          c = fgetc(f);
          if(c != EOF) buffer[pos++] = (char)c;
          if(pos >= size - 1) { // increase buffer length - leave room for 0
            size *=2;
            buffer = (char*)realloc(buffer, size);
          }
        }while(c != EOF && c != ' ');
        buffer[pos] = 0;
        // line is now in buffer
        // handle_line(buffer);
      } while(c != EOF);
      fclose(f);         
    }
  
    char *code =(char *)malloc(size);
    strcpy(code,buffer );
    free(buffer);
  
    return code;
}

char* getMessage(){
  
    int size = 53, pos; //53 chars taking in account the NULL at the end
    int c;
    char *buffer = (char *)malloc(size);

    FILE *f = fopen("msgfile.txt", "r");
  
    if(f) {
      do { // read all lines in file (should be only one, the code...)
        pos = 0;
        do{ // read one line
          c = fgetc(f);
          if(c != EOF) buffer[pos++] = (char)c;
          if(pos >= size - 1) { // increase buffer length - leave room for 0
            size *=2;
            buffer = (char*)realloc(buffer, size);
          }
        }while(c != EOF && c != ' ');
        buffer[pos] = 0;
        // line is now in buffer
        handle_line(buffer);
      } while(c != EOF);
      fclose(f);         
    }
  
    char *code =(char *)malloc(size);
    strcpy(code,buffer );
    free(buffer);
  
    return code;
}

int getMessageSize(){
  
    int size = 53, pos; //53 chars taking in account the NULL at the end
    int c;
    char *buffer = (char *)malloc(size);
    int m_size = 0;

    FILE *f = fopen("msgfile.txt", "r");
  
    if(f) {
      do { // read all lines in file (should be only one, the code...)
        pos = 0;
        do{ // read one line
          c = fgetc(f);
          if(c != EOF) buffer[pos++] = (char)c;
          if(pos >= size - 1) { // increase buffer length - leave room for 0
            size *=2;
            buffer = (char*)realloc(buffer, size);
          }
        }while(c != EOF && c != ' ');
        buffer[pos] = 0;
        // line is now in buffer
        //handle_line(buffer);
        m_size = m_size +1;
      } while(c != EOF);
      fclose(f);         
    }
  
    return m_size;
}

char* getMessageLine(int chosen){
  
    int size = 53, pos; //53 chars taking in account the NULL at the end
    int c;
    char *buffer = (char *)malloc(size);
    char *picked_line = (char *)malloc(size);
    int m_size = 0;

    FILE *f = fopen("msgfile.txt", "r");
  
    if(f) {
      do { // read all lines in file (should be only one, the code...)
        pos = 0;
        do{ // read one line
          c = fgetc(f);
          if(c != EOF) buffer[pos++] = (char)c;
          if(pos >= size - 1) { // increase buffer length - leave room for 0
            size *=2;
            buffer = (char*)realloc(buffer, size);
          }
        }while(c != EOF && c != ' ');
        buffer[pos] = 0;
        // line is now in buffer
      
        m_size = m_size +1;
        if(m_size == chosen){
         handle_line(buffer);
         picked_line = handle_line_picked(buffer);
        }
      } while(c != EOF);
      fclose(f);         
    }
  
  
    return picked_line;
}

int main(int argc, char *argv[]) {
  
    int size = 53; //53 chars taking in account the NULL at the end
  
  
    //here i will save my secret code
    char *code =(char *)malloc(size);
    code = getCode();
  
    printf("%s", "got in my buffer the needed code to decode ");
    printf("%s", code);
    printf("%s", " ");
  
    //here i will save my secret message
    char *message =(char *)malloc(size);
    printf("%s", "all message lines ");
    message = getMessage();
    printf("%s", " ");

  
    //the problem is you need some kind of way to read an specific line,
    // moreover a way to count the lines on the message
  
    int m_size; // size of the message
    printf("%s", "all message lines count ");
    m_size = getMessageSize();
    printf("%d", m_size);
    printf("%s", " ");
  
    //if you want an specific line of the message, you can iterate over MessageLine

    char *messageLine =(char *)malloc(size);
    printf("%s", "selected Line 12 ");
    messageLine = getMessageLine(12);
    printf("%s", " ");


  
    return 0;
}

------------- decodefile

CFL2AP(T9W,H!JEM86NDOBZ7UK1Y3VG4XR)ISOQ5.         ;-

--------------- msgfile.txt

19546
14501
17309
13027
16149
21512
18035
14014
15700
12515
16514
18207
13407
14837
16842
21037
15333
12244
21224
16321
14146
16014
20727
12804
18811
13711