You are to write a program that decodes an encoded message. Messages are decoded
ID: 3591210 • Letter: Y
Question
You are to write a program that decodes an encoded message.
Messages are decoded by subtracting 5 from the ASCII integer representation of the character in a string. For example, suppose an encoded string is “Mjqqt”. Then, the decoding process would be as follows:
Encoded Message
M
j
q
q
t
ACII
72
101
108
108
111
Subtract
5
5
5
5
5
Decoded Message
H
e
l
l
o
You are to write a program that decodes “N%knlzwji%ny%tzy&&&&&”.
You output can be just the decoded message
HINT:
Recall, that you can cast 1) a character to an integer or 2) an integer to character. See Slide 61 of Chapter 2 PowerPoint slides. For example,
char ch1 = ‘B’;
char ch2;
int x;
x = (int) ch1; // stores 66 in x
ch2 = (char) x + 5; // stores the letter G in ch2
See the charAt method in the String class
You can use the plus sign (i.e., +) to concatenate a character to a string
Encoded Message
M
j
q
q
t
ACII
72
101
108
108
111
Subtract
5
5
5
5
5
Decoded Message
H
e
l
l
o
Explanation / Answer
The program in C, I used string.h header file for string functions
#include<stdio.h>
#include<string.h>
int main() {
char *encode="N%knlzwji%ny%tzy&&&&&"; // this is the pointer pointing to string
char decode[100]; // this char array store decoded string
int i,n,ascii;
n= strlen(encode); // Find the length of encoded string
for(i=0;i<n;i++){ // read one by one character of encoded string
ascii = (int)encode[i]-5; // this convert the endcoded string character to ascii -5
decode[i]=(char)ascii; // convert to ascii to character and store into decode char array
}
puts(decode); // this is string function print the decoded string
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.