Question

In: Computer Science

Do not use global variables. Note : The interleave function should not accept input or display...

Do not use global variables.

Note : The interleave function should not accept input or display any output. The function should work with any size strings.

Caution : Inclusion of unnecessary or unrelated logic or code segment will result in loss of points.

Write C a program that has a main function and a utility function called merge. The main function should prompt the user to enter one short string (no longer than 20 characters) and hard code another string with value "123456" (this could be anything, not exceeding 20 characters). It should call the merge function that will interleave the first string and the second string to produce a third string. The merge stops with the shortest string. The main program should print out all the three strings after the function call.

Use as many variables and arrays as needed.

Expected input/output: ( do not worry about the exact number of blank lines in the output)

Scenario 1


Enter string 1 : ABCDE

Merged Result

String 1 : ABCDE

String 2 : 123456

Merged string : A1B2C3D4E5

Scenario 2

Enter string 1 : ABCDEFG

Merged Result

String 1 : ABCDEFG

String 2 : 123456

Merged string : A1B2C3D4E5F6

Solutions

Expert Solution

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//FUNCTION TO MERGE 2 STRINGS ALTERNATIVELY
char* merge(char a[], char b[]) {
int alen = strlen(a);
int blen = strlen(b);
int len=0;//len is length of merged string
if(alen==blen)
{
len = alen + blen;
}
else if(alen<blen)
{
len=2*alen;
}
else
{
len=2*blen;
}
char *arr;
int i = 0, j = 0, k = 0;
//Merging 2 strings
while(i < alen && j < blen)
{
arr[k++] = a[i++];
arr[k++] = b[j++];
}
arr[len]='\0';
return arr;//return merged string
}
int main() {
char string1[20];
char string2[]="123456";
char *res;
printf("Enter string1:");
scanf("%s",string1);
printf("Merged Result\n");
printf("String1 :%s\n",string1);
printf("String2 :%s\n",string2);
res =merge(string1,string2);
printf("Merged string :%s",res);
return 0;
}


Related Solutions

Write a program that accept an integer input from the user and display the least number...
Write a program that accept an integer input from the user and display the least number of combinations of 500s, 100s, 50s, 20s, 10s, 5s, and 1s. Test your solution using this samples] a. Input: 250 Output: 1x200s, 1x50s b. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s c. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s d. Input: 19 Output: 1x10s, 1x5s, 4x1s ​[Hints] o Use division to determine the number of occurrence of each element (i.e. 200, 100)...
Hints: In order to display all the combinations of input, you should use “Force” or “Clock”...
Hints: In order to display all the combinations of input, you should use “Force” or “Clock” to cover all the combinations of the inputs. For example, for input A, B, C, the waveforms below cover all combinations. (Similar to all the combinations in a truth table.) In your submission, please also show your VHDL program (in text or screen capture). You may also show the transcript in the screen to confirm the compilation of the program is successful: 1. Write...
Write a program that encrypts and decrypts the user input. Note – Your input should be...
Write a program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x) given by...
Use python write a function that translates the input string into Pig Latin. The translation should...
Use python write a function that translates the input string into Pig Latin. The translation should be done word by word, where all words will be separated by only one space. You may assume that each word must have at least one vowel (a,e,i,o,u and uppercased counterparts), and there will be no punctuation or other special characters in the input string. The Pig Latin rules are as follows: For words that begin with consonants, all letters before the initial vowel...
Compute the PI statistic for Project X and note whether the firm should accept or reject...
Compute the PI statistic for Project X and note whether the firm should accept or reject the project with the cash flows shown as follows if the appropriate cost of capital is 10%. Time 0 1 2 3 4 5 Cash Flow -250 75 0 100 75 50 a) 2.41, accept b) 0.023, reject c) 0.90, reject d) 1.97, accept
Compute the PI statistic for Project X and note whether the firm should accept or reject...
Compute the PI statistic for Project X and note whether the firm should accept or reject the project with the cash flows shown below if the appropriate cost of capital is 8 percent. Time: 0, 1, 2, 3, 4, 5 Cash Flow: -82, -82, 0, 117, 92, 67 Time: 0 1 2 3 4 5 Cash flow: -82 -82 0 117 92 67 Multiple Choice 48.17 percent, reject 8.00 percent, accept 58.74 percent, accept 47.21 percent, reject All information is...
1.      Compute the IRR for Project X and note whether the firm should accept or reject...
1.      Compute the IRR for Project X and note whether the firm should accept or reject the project with the cash flows shown as follows if the appropriate cost of capital is 9 percent. Time: 0 1 2 3 4 5 Cash flow: ?1,000 ?75 100 100 0 2,000 A.      9 PERCENT, ACCEPT B.       9 PERCENT, REJECT C.       16.61 PERCENT, ACCEPT D.      16.61 PERCENT, REJECT
Compute the PI statistic for Project X and note whether the firm should accept or reject...
Compute the PI statistic for Project X and note whether the firm should accept or reject the project with the cash flows shown below if the appropriate cost of capital is 10 percent.   Time: 0 1 2 3 4 5   Cash flow: -75 -75 0 100 75 50
Compute the PI statistic for Project X and note whether the firm should accept or reject...
Compute the PI statistic for Project X and note whether the firm should accept or reject the project with the cash flows shown below if the appropriate cost of capital is 10 percent.   Time: 0 1 2 3 4 5   Cash flow: -80 -80 0 115 90 65   
Compute the PI statistic for Project X and note whether the firm should accept or reject...
Compute the PI statistic for Project X and note whether the firm should accept or reject the project with the cash flows shown below if the appropriate cost of capital is 6 percent. Time: 0 1 2 3 4 5 Cash flow:-84 -84 0 109 84 59
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT