1 answer

Write a C program that takes two sets of characters entered by the user and merge...

Question:

Write a C program that takes two sets of characters entered by the user and merge them character by character.

Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle 

Your program should include the following function:

void merge(char *s3, char *s1, char *s2); 

The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer or shorter than the second set. The characters in s1 or s2 left after merging should be appended to the resulting string s3.

1) Assume each input is no longer than 1000 characters.

2) The merge 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 (below)

int read_line(char *str, int n)

{

     int ch, i = 0;

     while ((ch = getchar()) != '\n')

     { if (i < n)

        { *str++= ch;

           i++;

         }

     }

     *str = '\0';   /* terminates string */

     return i;        /* number of characters stored */

Thank you very much in advance!


Answers

#include "stdio.h"
int read_lint(char* ,int);                   // declarte the function to read line
void merge(char*,char*,char*);               // declate the function to merge
int main(void) {
int n=1000;                               // set max characters n = 1000
char a1[n];                               // declarate arrays a1,a2,a3;
char a2[n];
char a3[n+n];
char *str1=a1;                           // set pointers to arrays s1,s2,s3
char *str2=a2;
char *str3=a3;
merge(str3,str1,str2);                   // call the merge function.
printf("%s\n",str3);                   // print merged string
return 0;
}
void merge(char *str3,char *str1,char *str2){
   int l1,l2,l3,min,i,j,n=1000;                       // declare variables
printf("Enter the first set of characters:");
l1 = read_line(str1,n);                               // get input string
printf("Enter the second set of characters:");
l2 = read_line(str2,n);                               // get input string
min = l1>l2?l2:l1;                                   // calculate the min length of two strings
for(i=0,j=0;i<min;i++){                               // until min length
   *(str3+ j++) = *(str1+i);                       // add character to str3 from str1
   *(str3+ j++) = *(str2+i);                       // add character to str3 from str2
}
while(l1>i){                                       // if str1 has more characters add it to str3
   *(str3 + j++) = *(str1+ i++);
}
while(l2>i){                                       // if str2 has more characters add it to str3
   *(str3 + j++) = *(str2+ i++);
}
}

int read_line(char *str, int n)
{
int ch, i = 0;
while ((ch = getchar()) != '\n')
{ if (i < n)
{ *str++= ch;
i++;
}
}
*str = '\0'; /* terminates string */
return i; /* number of characters stored */
}

/* sample output

Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl defend the castle 

*/

.

Similar Solved Questions

1 answer
How do you graph #y=1/2x-5# by plotting points?
How do you graph #y=1/2x-5# by plotting points?...
1 answer
Em #6: Evaluate the following improper integral. 1, 63-2033 die If the integral does not converge,...
em #6: Evaluate the following improper integral. 1, 63-2033 die If the integral does not converge, then write "divergent" (without the quotes). Problem #6: Enter your answer symbolcally as in these examples If the integral does not converge, then write "divergent". Problem #8: Which ...
1 answer
Problem #2 (30): On July 1, 2020 GIFFCO contracted with the investment bank X. M. Retzoff...
Problem #2 (30): On July 1, 2020 GIFFCO contracted with the investment bank X. M. Retzoff et freres to issue $60 m of two-year notes at 97.6. The securities carry a 4.30 % coupon rate, and pay at the end of each quarter. Fees, commissions and other issue costs were $960,000. Required: Book the 7/1/2...
1 answer
Could you solve this with explaining ? Wright Company deposits all cash receipts on the day...
could you solve this with explaining ? Wright Company deposits all cash receipts on the day when they are received and it makes all cash payments by check. At the close of business on May 31, 2013, its Cash account shows a $30,700 debit balance. The company's May 31 bank statement shows $29,0...
1 answer
Ser PSE10 2.TPS.OP.003. My Notes Ask Your Teacher Previous Answers 7. 0/14 points One member of...
Ser PSE10 2.TPS.OP.003. My Notes Ask Your Teacher Previous Answers 7. 0/14 points One member of your group owns a sports car and has recorded some data for the straight-line motion of the car. The figure below shows part of the velocity data for the motion (m/s) а 50 40 30 20 10 t(s) 10 20 30 ...
1 answer
View Policies Current Attempt in Progress Fire Out Company manufactures its product, Vitadrink, through two manufacturing...
View Policies Current Attempt in Progress Fire Out Company manufactures its product, Vitadrink, through two manufacturing processes: Mixing and Packaging. All materials are entered at the beginning of each process. On October 1, 2020, inventories consisted of Raw Materials $26,000, Work in Process -...
1 answer
Classify each of the following substances as either a strong or weak acid, strong or weak...
Classify each of the following substances as either a strong or weak acid, strong or weak base, or a soluble or insoluble salt. H2CO3 CuS HNO Ca (OH) Mn3(PO4) strong acid weak acid strong base weak base soluble salt insoluble salt...
1 answer
Discuss how each of these factors (inflation, changing population demographics, intensity, and technology of services) influence...
Discuss how each of these factors (inflation, changing population demographics, intensity, and technology of services) influence health care costs....
1 answer
36. All are considered antioxidants except: a vitamin A b. vitamin C c. selenium d. vitamin D 37. Which of the...
36. All are considered antioxidants except: a vitamin A b. vitamin C c. selenium d. vitamin D 37. Which of the following foods does not contribute a significant amount of beta-carotene? a. pineapple b. sweet potato c. mango d. spinach volls e. squash vode odoo pinto 38. All are risk factors associat...
1 answer
How do you use the product rule to differentiate #f(t) = (1 - t^2) (1 - 3/t^2)#?
How do you use the product rule to differentiate #f(t) = (1 - t^2) (1 - 3/t^2)#?...
1 answer
Page 10 of 20 Next Page Question 10 (6 points) A brewery pulls 1070 bottles of...
Page 10 of 20 Next Page Question 10 (6 points) A brewery pulls 1070 bottles of beer to sample them for quality control. In the past, there has been a 0.13 probability that a bottle of beer is defective. Assuming this trend continues, what is the probability that 134 or fewer bottles of beer in this ...
1 answer
24. Circle one word inside each set of parentheses in the following statement: (Solutions, colloids, suspensions)...
24. Circle one word inside each set of parentheses in the following statement: (Solutions, colloids, suspensions) contain the smallest particle size and (solutions, colloids, suspensions) contain the largest particle size. Which statement about colligative properties is incorrect? A. Solutes will lo...
1 answer
Citor, in series with a resistor, is being charged. At the end of 10 ms its...
citor, in series with a resistor, is being charged. At the end of 10 ms its charge is half the final 7) A certain capa value. Find the time constant (RC). n (charge--I 6 x 10-19 C) İs moving in the xy plane. the components of its velocit...
1 answer
What does the word "Karma" mean?
What does the word "Karma" mean?...
1 answer
How can you clean your intestines?
How can you clean your intestines?...
1 answer
Think of a relevant example in your own life of how a change in the market...
Think of a relevant example in your own life of how a change in the market (including information, preferences, technology, price of alternative goods, regulations, taxes, etc.) has shifted either the supply or demand of a good. How did this change affect the market equilibrium for that good or serv...