In: Computer Science
2. Implement a decoder for the encoding scheme used in the first problem. The decoder will reverse whatever operation was performed on the string, meaning each character will be rotated 3 to the left. For example, the input string def will decode to abc. Create a program which implements this decoding scheme and allows the user to input a string to be decoded. • Only use ASCII characters in range [32, 127]. • Read an entire string of input from the user using fgets. • Use input prompt "Enter a string: ". • Remove the trailing newline character from the string. • Encode the string using the scheme described above. • Output ONLY the decoded message on a new line. • Save your code as decoder.c.
How to write this in C language
//IF YOU ARE SATISFIED WITH THE CODE, KINDLY LEAVE A LIKE, ELSE COMMENT TO CLEAR DOUBTS. PLEASE DO NOT DOWN VOTE WITHOUT ANY REASON, IF I CANNOT CLARIFY THEN DOWNVOTE
CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char string[50];
printf("\nEnter the string to be decoded: ");
fgets(string, 100, stdin);
int size = strlen(string) -1;
int k;
for (int i = 0; i < size; i++)
{
k = (int)(string[i]);
k -= 3;
if (k < 32)
{
k = 127 - (32 - k);
}
string[i] = (char)k;
}
printf("\nDecoded -> %s", string);
return 0;
}
OUTPUT: