Question

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: This is my first program

Hello worldThis is my first program

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *stringMerge(char s1[], char s2 []){
    int length1 = strlen(s1);
    int length2 = strlen(s2);
    int i = 0, j = 0, k = 0;
    char* result = (char*) malloc(sizeof(char)*(length1+length2+1));
    while(i<length1){
        result[k++] = s1[i++];
    }
    while(j<length2){
        result[k++] = s2[j++];
    }
    result[k] = '\0';
    return result;
}

int main()
{
    char s1[100], s2[100];
    char* result;
    
    printf("Enter the first string: ");
    scanf("%[^\n]%*c", s1);
    
    printf("Enter the second string: ");
    scanf("%[^\n]%*c", s2);
    
    result = stringMerge(s1,s2);
    
    printf("%s",result);
    
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
C programming 1 String Merging Write a program that reads two strings s1 and s2 from...
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 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...

  • You are given a list of strings L = {s1,s2 ...sn}. Each si is a string...

    You are given a list of strings L = {s1,s2 ...sn}. Each si is a string of up to 26 English characters. For example, L consists of the following strings: {s1, 82... Sn}. Each s; is a string of up to 26 English 13. You are given a list of of strings L = characters. For example L consists of the following strings: S1 =hello', S2 = 'world, s3 = 'what', s4 = 'a', 85 = 'nice', s6 = 'day',...

  • ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring...

    ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. int indexOf(const string& s1, const string& s2) Write a test program that reads two strings and checks whether the first string is a substring of the second string.

  • Write a program that uses String method regionMatches to compare two strings input by the user....

    Write a program that uses String method regionMatches to compare two strings input by the user. The program should prompt the user to enter two strings, the starting index in the first string, the starting index in the second string, and the number of characters to be compared. The program should print whether or not the strings are equal, (Ignore the case of the characters during comparison.) 四SAMPLE RUN #1: java StringCompare Highlight: None D Show Highlighted Only Interactive Session...

  • ****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...

  • Given two C-string s1 and s2, write an if - else if - else statement to...

    Given two C-string s1 and s2, write an if - else if - else statement to print out message to indicate which string goes first in lexicographic order or two strings are the same. It must. Be if-else if -else statement c++

  • Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if...

    Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if s1 is the reverse of s2, false otherwise. Then, draw the sequence of recursive calls for the following cases. Submit your diagrams in a PDF file called isReverseTrace.pdf. isReverse("happy", "yppah") will return true isReverse("cool", "loac") will return false isReverse("", "") will return true

  • Write a program that reads a string from the keyboard and computes the two arrays of...

    Write a program that reads a string from the keyboard and computes the two arrays of integers upperCase, and lowerCase, each of size 26. The first one represents the frequency of upper case letters and the second one represents the frequency of lower case letters in the input string. After computing these arrays, the program prints the frequencies in the following format: The frequency of the letter A= The frequency of the letter B= Assume the input string contains only...

  • Write a NASM assembly language(run it on DOSBOX) procedure using string instruction. Given two strings, S1...

    Write a NASM assembly language(run it on DOSBOX) procedure using string instruction. Given two strings, S1 and S2, determine if S2 is a sub string of S1. Sub string example: S1: ‘I am happy today’, S2: ‘happy’, S2 is a substring of S1. S1: ‘I am happy today’, S2: ‘sad’, S2 is not a substring of S1. Call above procedure in an assembly language program and show the result of the procedure.

  • Write the function in python and show the code 2. Write a function jscore (s1, s2)...

    Write the function in python and show the code 2. Write a function jscore (s1, s2) that takes two strings s1 and s2 as inputs and returns the Jotto score of s1 compared with s2 -.e., the number of characters in s1 that are shared by s2. The positions and the order of the shared characters within each string do not matter. Repeated letters are counted multiple times, as long as they appear multiple times in both strings. For example...

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