Question

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

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!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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

*/

Add a comment
Know the answer?
Add Answer to:
Write a C program that takes two sets of characters entered by the user and merge...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • C programming 1 String Merging Write a program that reads two strings s1 and s2 from...

    C programming 1 String Merging Write a program that reads two strings s1 and s2 from the keyboard and merges them to a string s3. Strings s1 and s2 are statically allocated whereas s3 is dynamically allocated to fit exactly s1 and s2. The two original strings are no longer than 100 characters long. Use the following function to merge the two strings. char *stringMerge(char s1[], char s2 []); Example: Enter the first string: Hello world Enter the first string:...

  • Write a program that reads a sentence input from the user. The program should count the...

    Write a program that reads a sentence input from the user. The program should count the number of vowels(a,e,i,o,u) in a sentence. Use the following function to read the sentence. Should use switch statement with toupper. int read_line(char str[],int n) {    int ch, i=0;    while((ch =getchar()) != '\n')        if(1<n)            str[i++]==ch;    str[i] != '\0';    return i; } output should look like: Enter a sentence: and thats the way it is! Your sentence...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • ****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of...

    ****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of a file. The program encoded sentences in the file and writes the encoded sentences to the output file. Enter the file name: messages.txt Output: encoded words are written to file: messages.txt.swt The program reads the content of the file and encodes a sentence by switching every alphabetical letter (lower case or upper case) with alphabetical position i, with the letter with alphabetical position 25...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • // Write a program that determines how many of each type of vowel are in an...

    // Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less. // The program should prompt the user for a string. // The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string. // Vowels: a, e, i, o, u. // Output the entered string, how many of...

  • Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line...

    Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line is read, the program strips out all characters that are not upper or lower case letters or spaces, and then outputs the line. Thus, the program acts as a filter and issues no prompt. There are many ways this program could be written, but to receive full credit, you must observe the following: Place your code in a file called filterChars.cpp. The program should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT