Question
One way to encode a word is to split it into two sets of characters one by one and merge the second set before the
One way to encode a word is to split it into two sets of characters one by one and merge the second set before the first set one by one. Write a C program encode.c that implements this algorithm. For example, a word defend will be split into two sets of characters: dfn and eed. Merging the two sets of characters one by one with the second set before the first set will result in: edefdn.
The two sets will be either the same length (when the input word has even number of characters) or the first set will be one character longer than the second set (when the input word has odd number of characters). If the first set is longer than the second set, the last character in the first set will be appended to the result. For example, Enter a word: merging Output: emgrnig
Your program should include the following function: void encode(char *s1, char *s2); The function expects s2 to point to a string containing a string that is converted from s1.
1) Assume each 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) in the lecture notes.
read_line function
int read_line(char *str, int n) { int ch, i = 0; while ((ch = getchar()) != ' ') { if (i < n) { *str++= ch; i++; } } *str = '\0'; return i; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started