Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

int read_line(char *str, int n) { int ch, i = 0; while ((ch = getchar()) != ' ') { if (i { *str++= ch; i++;

image text in transcribed
int read_line(char *str, int n)
{
int ch, i = 0;
while ((ch = getchar()) != ' ')
{ if (i
{ *str++= ch;
i++;
}
}
*str = '\0'; /* terminates string */
return i; /* number of characters stored */
}
1. (70 points) 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 *sl, 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 [Il operator in the function. 3) To read a line of text, use the read_line function (the pointer version) in the lecture notes

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions

Question

Name and describe the three main types of business activities.

Answered: 1 week ago