Joe wants a way to encode his notes he passes in class so his teacher cannot rea
ID: 3586402 • Letter: J
Question
Joe wants a way to encode his notes he passes in class so his teacher cannot read them. Write a C 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. For example:
Input: at the cafeteria
Output: zg gsv xzuvgvirz
Your program should include the following function:
void convert(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) Name your program notes.c.
2) Assume input is no longer than 1000 characters.
3) The convert 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.
4) To read a line of text, use the read_line function (the pointer version).
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void convert(char *s1, char *s2){
int j,i,k;
char a = 'a';
for(i = 0; i<strlen(s1); i++){
*(s2+i) = *(s1+i);
if (*(s1+i) >= 'a' && *(s1+i) <= 'z'){
j = 25-(*(s1+ i) - 97);
a = 'a';
for (k=0; k<j; k++){
a++;
}
*(s2+i) = a;
}
if (*(s1+i) >= 'A' && *(s1+i) <= 'Z'){
j = 25-(*(s1+ i) - 65);
a = 'A';
for (k=0; k<j; k++){
a++;
}
*(s2+i) = a;
}
}
*(s2+i) = '';
}
int main(){
char *line;
size_t len = 1000;
char str1[1000];
line = (char *)malloc(sizeof(char) * 1000);
getline(&line,&len,stdin);
convert(line,str1);
printf("%s",str1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.