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

Write a C program coding.c that includes a main program and two functions with t

ID: 3828081 • Letter: W

Question

Write a C program coding.c that includes a main program and two functions with the following function prototypes:

void encodeStr ( char *input, char *output, CodeT code );

void decodeStr ( char *input, char *output, CodeT code );

The first function takes the input string in input and generates the encoded string using the information in code. The result is stored in output. The second function takes the input string in input and decodes it back using the information in code. The result is stored in output again.

Both functions use the following structure as the key to encode and decode text messages:

typedef struct {

   char from[28];

   char to[28];

} CodeT;

The main function may then initialize the values as shown below:

CodeT code = {   .from = " abcdefghijklmnopqrstuvwxyz",

                 .to = ".172093%@#+!:_-$^*()854=6?>" };

The main program must prompt the user repeatedly for a line of text and then generate and print the encoded text string before decoding the encoded text string and printing the result again to the screen to test that the original text string can be retrieved through the decoding function. The program should stop prompting the user for input text when the user enters an empty text as input. Use the program discussed in class to read text lines from the keyboard.

Explanation / Answer


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

typedef struct
{
char from[28];
char to[28];
}CodeT;

void encodeStr ( char*, char*, CodeT);
void decodeStr ( char*, char*, CodeT);
int findCharPos(char, char*);
int main()
{
char input[50];
char output[50];


CodeT ref = { .from = " abcdefghijklmnopqrstuvwxyz",
.to = ".172093%@#+!:_-$^*()854=6?>" };


for(;;){
printf("Enter the string for encrypting ");
fgets(input,sizeof(input),stdin);
if (strcmp (input, " ") == 0)
{
printf("You Pressed Enter..Process End..");
return 0;
  
}


encodeStr(input,output,ref);
printf("The encoded string is : %s ", output);


printf("Enter the string for decoding ");
fgets(input,sizeof(input),stdin);


decodeStr(input,output,ref);
printf("The decoded string is : %s ", output);

}
return 0;

}


int findCharPos(char ch, char *ref)
{
int i,length_ref;

length_ref = strlen(ref);

for(i = 0; i < length_ref; i++)
{
if(ref[i] == ch)
return i;
}
return -1;
}
void encodeStr(char *input, char *output, CodeT code)
{
int length_input,i,pos;
length_input = strlen(input);

for(i = 0; i < length_input; i++)
{
pos = findCharPos(input[i],code.from);
output[i] = code.to[pos];
}
output[i] = '';
}
void decodeStr(char *input, char *output, CodeT code)
{
int length_input,i,pos;

length_input = strlen(input);

for(i = 0; i < length_input; i++)
{
pos = findCharPos(input[i],code.to);
output[i] = code.from[pos];
}
output[i] = '';
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote