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

Program Using C. Create a program called encode.c. Mike has been passing notes i

ID: 3853188 • Letter: P

Question

Program Using C.

Create a program called encode.c.   Mike has been passing notes in class without a problem until his teacher intercepted one day. In order to prevent future embarrassment, he wants a way to encode his notes so his teacher cannot read them. Write a program that encodes a sentence by switching every alphabetical letter (lower case or upper case) with alphabetical position i with the letter with alphabetical position 25 – i. For example, letter a is at position 0 and after encoding it becomes letter z (position 25). Letter m is at position 12, it becomes letter n (25 – 12 = 13) after encoding.

Input: Meet me at the cafeteria

Output: Nvvg nv zg gsv xzuvgvirz

Your program should include the following function:

void encode(char *s1, char *s2);

The function expects s1 to point to a string containing the input as a string and stores the output to the string pointed by s2.

1) Assume input is no longer than 1000 characters.

2) The encode function should use pointer arithmetic (instead of array subscripting). In other words, eliminate the loop index variables and all use of the [] operator in the function.

3) To read a line of text, use the read_line function (the pointer version)

Explanation / Answer

#include <stdio.h>
void read_line(char *s1) {
printf("Enter the line of text: ");
gets(s1);
}
void encode(char *s1, char *s2) {
char ch[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int i, j,index;
int isUpper = 0;
for(i=0; *(s1+i) != ''; i++) {
index = -1;
isUpper = 0;
if(*(s1+i) >= 'A' && *(s1+i) <= 'Z') {
*(s1+i) = *(s1+i) + 32;
isUpper = 1;
}
for(j=0;j <26; j++) {
if(ch[j] == *(s1+i)) {
index = j;
break;
}
}
if(index != -1) {
if(isUpper ==1 ){
*(s2+i) = ch[25-index]-32;
} else {
*(s2+i) = ch[25-index];
}
} else {
*(s2+i) = *(s1+i);
}
}
*(s2+i) = '';
}
int main()
{
char s1[1000], s2[1000];
read_line(s1);
encode(s1,s2);
printf("Encoded string is: %s ",s2);
return 0;
}

Output:

sh-4.2$ main                                                                                                                                                                                         

Enter the line of text: Meet me at the cafeteria                                                                                                                                                     

Encoded string is: Nvvg nv zg gsv xzuvgvirz

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote