Joe wants a way to encode his notes he passes in class so his teacher cannot rea
ID: 3587742 • 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) as shown in example below.
int read_line(char*str, int n)
{
int ch, i = 0;
while ((ch = getchar()) != ' ')
{ if (i < n)
{ *str++=ch;
i++;
}
}
*str= ''; /* terminates string */
return i; /* number of characters stored */
}
Explanation / Answer
In my system enter is not ' ' so readline is not working properly. but i am sure that my convert function will work porperly.
Go threw this code.
#include <stdio.h>
#include <stdlib.h>
int read_line(char * str,int n)
{
int ch,i=0;
while((ch = getchar()) != ' ')
{
printf("in getchar ");
if(i<n)
{
*str++=ch;
i++;
}
}
*str=''; /*terminates string*/
return i;/*number of characters stored*/
printf("line is %d ",i);
}
void convert(char const * s1 ,char * s2)
{
int position =0;
while(*s1 != ' ')
{
if(*s1 >= 'A' && *s1 <= 'Z') //If capital latter come change into small
*s2 = *s1 + 32 ;
position = *s2 - 97; // find the postion of latter
position = 25 - position;
*s2 = 'a'+position; // add modified postion
s1++;
s2++;
}
}
void main(void)
{
char *strIn , *strOp;
strIn= (char *) malloc(1000 * sizeof(char)); //allocate space for input string
strOp= (char *) malloc(1000 * sizeof(char)); // alocate space for output string
read_line(strIn,1000); // read from user
convert(strIn,strOp); // encode sting
printf("Input - %s output %s ",strIn,strOp); // print output
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.