Need some help programming this code in basic C I need to ad

Need some help programming this code in basic C!

I need to add the code to the places labeled /*********Enter Code*********/. Thanks!!

You and your roommate are creating a secret code.

You have a rather simple scheme to express strings of letters as numbers. You realize that since there are only 26 different letters, a string of letters can be viewed as a number in base 26! For example, consider the word

COMPUTER

You realize that we can just assign numeric values to the letters with A = 0, B = 1, C = 2, …, Z = 25 and then treat each letter location as the ones place, the 26 place, the 262 place and so forth. Thus, the value of COMPUTER would be:

2x26^7 + 14x26^6 + 12x26^5 + 15x26^4 + 20x26^3 + 19x26^2 + 4x26^1 + 17x26^0.

Function Details: Decode/PrintText

When attempting to reverse this process, note that we can repeatedly mod by 26 and divide by 26 to peel off each letter from the end, one by one. Rather than provide the code, a small example will be illustrated below:

Consider the number 1371, knowing the length of the word it encoded was 3:

1371 % 26 = 19 (T),                1371 / 26 = 52

52 % 26     =   0 (A),                52 / 26     =   2

2   % 26     =   2 (C)                 2 / 26      = 0

The corresponding message was \"CAT\", reading the letters from the bottom to the top.  

Note that to take an integer, x, in between 0 and 25, here is how you can store the corresponding uppercase letter in the character array word, index i:

word[i] = (char)(x + \'A\');

Don\'t forget to null terminate your string after you create it.

Here is the skeleton code....

#include

#include

#define MAX_LEN 14

long long convert(char word[], int length);

void printText(long long value, int length);

int main() {

int choice;

printf(\"Would you like to (1) encode or (2) decode?\ \");

scanf(\"%d\", &choice);

// Encode.

if (choice == 1) {

// Get input file name.

char filename[MAX_LEN];

printf(\"Please enter the input file name, to encode.\ \");

scanf(\"%s\", filename);

FILE* ifp = fopen(filename, \"r\");

// Convert each word, one by one.

while (!feof(ifp)) {

char word[MAX_LEN];

fscanf(ifp, \"%s\", word);

int len = strlen(word);

long long res = convert(word, len);

printf(\"%lld %d\ \", res, len);

}

fclose(ifp);

}

else {

// Get input file name.

char filename[MAX_LEN];

printf(\"Please enter the input file name, to decode.\ \");

scanf(\"%s\", filename);

FILE* ifp = fopen(filename, \"r\");

// Convert each pair of numbers, one by one.

while (!feof(ifp)) {

long long value;

int len;

fscanf(ifp, \"%lld%d\", &value, &len);

printText(value, len);

}

fclose(ifp);

}

return 0;

}

long long convert(char word[], int length) {

   /*** Remove this statement and fill in this function. ***/

}

void printText(long long value, int length) {

char word[MAX_LEN];

/*** Fill in this code. Populate the word array by filling in each

character, backwards, using the algorithm shown in the program

write up. Remember to to null terminate your string appropriately.

***/

printf(\"%s\ \", word);

return;

}

Solution

#include<stdio.h>
#include<string.h>
#include<math.h>
#define MAX_LEN 14
#define Numfordecode = 26

long long convert(char word[], int length);
void printText(long long value, int length);
const double NumforEncode = 26;
char decode[]={\'A\',\'B\',\'C\',\'D\',\'E\',\'F\',\'G\',\'H\',\'I\',\'J\',\'K\',\'L\',\'M\',\'N\',\'O\',\'P\',\'Q\',\'R\',\'S\',\'T\',\'U\',\'V\',\'W\',\'X\',\'Y\',\'Z\'};

int main() {
int choice;

printf(\"Would you like to (1) encode or (2) decode or 3 to exit?\ \");
scanf(\"%d\", &choice);
// Encode.
if (choice == 1) {
// Get input file name.
char filename[MAX_LEN];
printf(\"Please enter the input file name, to encode.\ \");
scanf(\"%s\", filename);
FILE* ifp = fopen(filename, \"r\");
// Convert each word, one by one.
while (!feof(ifp)) {
char word[MAX_LEN];
fscanf(ifp, \"%s\", word);
int len = strlen(word);
long long res = convert(word, len);
printf(\"%lld %d\ \", res, len);
}
fclose(ifp);
}
else if(choice == 2) {
// Get input file name.
char filename[MAX_LEN];
printf(\"Please enter the input file name, to decode.\ \");
scanf(\"%s\", filename);
FILE* ifp = fopen(filename, \"r\");
// Convert each pair of numbers, one by one.
while (!feof(ifp)) {
long long value;
int len;
fscanf(ifp, \"%lld%d\", &value, &len);
printText(value, len);
fclose(ifp);
}
}
else
{
   printf(\"Exiting......\");
}


return 0;
}
long long convert(char word[], int length) {
/*** Remove this statement and fill in this function. ***/
   int i = 0 ;
   long long sum=0 ;
   double cnt = length-1;

   for(int i= 0; i < length; i++ )
   {
       sum+= word[i] * pow(NumforEncode,cnt);
       --cnt;
   }
   return sum;
}
void printText(long long value, int length) {
char word[MAX_LEN],tmp;
long long rem;
int i= 0,j;
/*** Fill in this code. Populate the word array by filling in each
character, backwards, using the algorithm shown in the program
write up. Remember to to null terminate your string appropriately.
***/
do
{
   rem = value % 26;
   word[i] = (char)(rem+\'A\');
   //value= (value)/Numfordecode;
   i++;
}while( value/=26 );

word[i] = \'\\0\';
strrev(word);
printf(\"%s\ \", word);
return;
}

Need some help programming this code in basic C! I need to add the code to the places labeled /*********Enter Code*********/. Thanks!! You and your roommate are
Need some help programming this code in basic C! I need to add the code to the places labeled /*********Enter Code*********/. Thanks!! You and your roommate are
Need some help programming this code in basic C! I need to add the code to the places labeled /*********Enter Code*********/. Thanks!! You and your roommate are
Need some help programming this code in basic C! I need to add the code to the places labeled /*********Enter Code*********/. Thanks!! You and your roommate are

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site