Write a C program coding that includes a main program and tw

Write a C program coding that includes a main program and two functions with the following function prototypes: void encoder (char \'inpur, char \'ourpur, CodeT code); void decodeSrr (char \'inpur, char \'output, CodeT code); The first function takes the input string in inpur and generates the encoded string using the information in code. The result is stored in ourpur. The second function takes the input string in input and decodes it back using the information in code. The result is stored in ourpur 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 mam 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.

Solution

#include \"stdio.h\"
#include \"string.h\"
typedef struct{
   char from[28];
   char to[28];
}CodeT;

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

int main(void) {
// Disable stdout buffering
setvbuf(stdout, NULL, _IONBF, 0);
int i;
CodeT code ={ .from = \" abcdefghiklmnopqrstuvwxyz\",
.to = \".172093%@#+!:_-$^()854=6?>\"
};
char inp[100] = \" abcdefghiklmnopqrstuvwxyz\";
char outp[100];
encodeStr(&inp,&outp,code);
decodeStr(&outp,&inp,code);
for(i=0;i<strlen(inp);i++){
printf(\"%c\",inp[i]);
}
return 0;
}

void encodeStr(char *input, char *output, CodeT code){
int i,j;
int len = strlen(input);
// for each character in input
for(i=0;i<len;i++){
// check in each character of from in code and add to in code to output
for(j=0;j<28;j++){
if(input[i]==code.from[j]){
output[i] = code.to[j];
break;
}
}
}
}

void decodeStr(char *input, char *output, CodeT code){
int i,j;
int len = strlen(input);
for(i=0;i<len;i++){
// check in each character of from in code and add to in code to output
for(j=0;j<28;j++){
if(input[i]==code.to[j]){
output[i] = code.from[j];
break;
}
}
}
}

/* sample output
abcdefghiklmnopqrstuvwxyz
*/

 Write a C program coding that includes a main program and two functions with the following function prototypes: void encoder (char \'inpur, char \'ourpur, Code
 Write a C program coding that includes a main program and two functions with the following function prototypes: void encoder (char \'inpur, char \'ourpur, Code

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site