Program Using C. Create a program called encode.c. Mike has been passing notes i
ID: 3853350 • 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>
#include<stdlib.h>
void encode(char *s1, char *s2){
while(*s1!=''){
//for upper case letters
//ascii value - 65-90
// so for each i, value will be 90-i+65, to get a letter in the given range
if(*s1>=65&&*s1<=90){
*s2=90-*s1+65;
}
//for lower case letters
//ascii value - 97-122
// so for each i, value will be 97-i+122, to get a letter in the given range
else
if(*s1>=97&&*s2<=122)
{
*s2=122-*s1+97;
}
//for characters other than alphabets
else{
*s2=*s1;
}
//to change the pointer value to point to the next character
s1++;
s2++;
}
*s2='';
}
int main(){
char str1[1000], str2[1000];
FILE *fptr;
if((fptr=fopen("C:\Users\Dell\Desktop\encode.txt","r"))==NULL){
printf("ERROR ");
exit(1);
}
//Read_line function does not exist in C
while(fread(str1,1,1000,fptr)!=NULL){
printf("%s ",str1);
encode(str1,str2);
printf("%s ",str2);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.