Write a C program coding.c that includes a main program and two functions with t
ID: 3765728 • 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.
**The problem I'm having is that when I enter a text phrase like "What's up" , it doesn't give me a space on the decoded phrase it gives me a question mark and it doesn't insert a period (.) for the space in between the s and u.
Example output:
Please enter your message up to 80 lower-case letters or spaces long. Press ENTER when done.
whats up
The encoded message is:
=@18)5^
The decoded message is:
whats?up
**The other problem I am having is that I haven't quite figured the logic on how to get the program to stop when the user enters an empty text as input. That's why I have a prompt with a do while loop.
Any help would be great. Thank you!
This is the code I have so far:
1#include<stdio.h>
2 #include<stdbool.h>
3
4 typedef struct
5 {
6 char from[28];
7 char to[28];
8 }CodeT;
9
10 int main(void)
11 {
12 bool done = false;
13 char userInput;
14 do
15 {
16 CodeT code = { .from = " abcdefghijklmnopqrstuvwxyz", .to = ".172093%@#+!:_-$^*()854=6?>" };
17 char input[81];
18 char output[81];
19
20 void encodeStr ( char *input, char *output, CodeT code );
21 void decodeStr ( char *input, char *output, CodeT code );
22 void readLine(char *input);
23
24 printf("Please enter your message up to 80 lower-case letters or spaces long. Press ENTER when done. ");
25
26 readLine(input);
27
28 printf("The encoded message is: ");
29 encodeStr(input, output, code);
30
31 printf("%s ", output);
32
33 printf("The decoded message is: ");
34 decodeStr(input, output, code);
35
36 printf("%s ", output);
37
38 printf("Are you finished? N for no, Y for yes. ");
39 scanf("%c", &userInput);
40
41 if((userInput == 'Y') || (userInput == 'y'))
42 {
43 done = true;
44 getchar();
45 }
46 else if((userInput == 'n') || (userInput == 'N'))
47 {
48 done = false;
49 getchar();
50 }
51 }
52 while(done == false);
53 printf(" Thank you for using this program. Good-bye!");
54
55 return 0;
56 }
57
58 void encodeStr(char *input, char *output, CodeT code)
59 {
60 int i =0;
61 int j;
62 int x = 0;
63 do
64 {
65 for(j = 0; j < 81; j++)
66 {
67 if(input[i] == code.from[x])
68 {
69 output[i] = code.to[x];
70 i++;
71 x = 0;
72 }
73 x++;
74 }
75 }
76 while(input[i] != '');
77 }
78
79 void decodeStr(char *input, char *output, CodeT code)
80 {
81 int i =0;
82 int j;
83 int x = 0;
84 int y = 0;
85 while (output[x] != '')
86 {
87 input[x] = output[x];
88 x++;
89 }
90 do
91 {
92 for(j = 0; j < 81; j++)
93 {
94 if(input[i] == code.to[y])
95 {
96 output[i] = code.from[y];
97 i++;
98 y = 0;
99 }
100 y++;
101 }
102 }
103 while(input[i] != '');
104 }
105
106 void readLine(char *input)
107 {
108 char character;
109 int i =0;
110
111 do
112 {
113 character = getchar();
114 input[i] = character;
115 i++;
116 }
117 while(character != ' ');
118 input[i - 1] = '';
119 }
120
Explanation / Answer
Basically you are not terminating the string with ''.
For terminating the user input you can do the following. This will not necessarily read a line of text - it will stop reading at the first space. It is also prone to buffer overflows. There are things you can do to the "scanf()" to mitigate this, but it would be easier to just use "fgets()" to get a line of text.
Your encodeStr-function take code.to[pos] without checking the value of pos.pos can be -1 in the case that the character is not in code.from included.If pos -1, you must take the original character insteed of the code.to[pos].
'pos' is the position of the coded character
'i' is the position in the sting 'input' and 'output' where you are currently working on.At last, terminate the output with ''.
I am giving here your encodeStr() function corrected.
void encodeStr (char *input , char *output , CodeT code)
{
int i;
int input_l =strlen(input);
for (i=0 ; i<= input_l; i++)
{
int pos = search (input[i], code);
if (pos >= 0)
output[i] = code.to[pos];
else
output[i] = input[i];
printf("%C", output[i]);
}
output[i] = '';
printf(" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.