Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1 Write a program that asks the user to enter two strings (with a maximu

ID: 3711943 • Letter: P

Question

Problem 1
Write a program that asks the user to enter two strings (with a maximum length of 30) and then performs one of two
possible operations provided as command line arguments to the program. The possible command line options and
the associated operations are as follows:


• Option “-i”

Operation: if given this option, the program should create a new string that is built by
interspersing the two strings, by alternatively placing one character at a time from each string. When one of
the strings exhausted, the program should copy the remaining of the characters from the second string. This
functionality should be implemented in a function called intersperse, which takes as parameters the
two strings and returns a pointer to the newly created string. Your function should use dynamic memory
allocation to generate the new string.


For example, if string 1 is “abcde” and string 2 is “1234567”, the resulting string should be
“a1b2c3d4e567”.


• Option “-w”

Operation: if given this option, the program should create a new string that is built by
concatenating the two given strings, in which there has been a ‘*’ character inserted in between every
character in those strings. There should be a ‘*’ character between the two strings, but no such character
at the end of string 2. This functionality should be implemented in a function called widen_stars,
which takes as parameters the two strings and returns a pointer to the newly created string. Your function
should use dynamic memory allocation to generate the new string.


For example, if string 1 is “abcde” and string 2 is “1234567”, the resulting string should be
“a*b*c*d*e*1*2*3*4*5*6*7”.


Your program should function as follows (items underlined are to be entered by the user):
> combine_strings -i
Please enter a string of maximum 30 characters: abcde (underlined)
Please enter a string of maximum 30 characters: 1234567 (underlined)
The combined string is: a1b2c3d4e567


Or


> combine_strings -w
Please enter a string of maximum 30 characters: abcde (underlined)
Please enter a string of maximum 30 characters: 1234567 (underlined)
The combined string is: a*b*c*d*e*1*2*3*4*5*6*7

Important Notes:
1. Formatting: Make sure that you follow the precise recommendations for the output content and
formatting: for example, do not change the text in the first problem from “Please enter a string
of maximum 30 characters: ” to “Enter string: ”. Your assignment will be auto-graded
and any changes in formatting will result in a loss in the grade.


2. Comments: Header comments are required on all files, for each function, and recommended throughout
the rest of the program. Points will be deducted if no header/function are included.


3. Restriction: The use of goto statements anywhere within this program is prohibited. Points will be
deducted if goto is used.

4. You may not include the string library, but you may use the strlen and strcmp functions from the last
project

5. Before terminating, your program needs to de-allocate the space allocated for the newly created strings

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <string.h> /* used just for strlen and strcmp */

char * intersperse(char *str1 , char *str2){

size_t len1 = strlen(str1);

size_t len2 = strlen(str2);

char *str3 = malloc(len1+len2+1);

int i = 0 , k =0;

int j = 0;

while (str2[k]!='' && str1[j]!='' ){

if (str1[j]!=''){

str3[i] = str1[j];

j++;

i++;

}

if (str2[k] != ''){

str3[i] = str2[k];

i++;

k++;

}

}

str3[i] = '';

return str3;

}

char * widen_stars(char *str1 , char *str2){

size_t len1 = strlen(str1);

size_t len2 = strlen(str2);

char *str3 = malloc(2*(len1+len2+1));

int i = 0;

int j =0 , k=0;

while (str2[k]!='' && str1[j]!='' ){

if (str1[j] !=''){

str3[i] = str1[j];

j++;

i++;

}

str3[i] = '*';

i++;

if (str2[k] != ''){

str3[i] = str2[k];

i++;

k++;

}

str3[i] = '*';

i++;

}

str3[i] = '';

return str3;

}

int main (int argc , char *argv[] ) {

char str1[31] = {''};

char str2[31] = {''};

size_t len1 = strlen(str1);

size_t len2 = strlen(str2);

char *str3 = malloc (len1+len2+1);

char *str4 = malloc (2*(len1+len2+1));

printf("Please enter a string of maximum 30 characters : ");

fgets(str1 ,31 ,stdin );

printf("Please enter another string of maximum 30 characters : ");

fgets(str2 , 31 ,stdin );

if (!strcmp (argv[1],"-i")){

str3 = intersperse(str1 , str2 );

printf ("the combined string is : %s",str3);

}

if (!strcmp (argv[1] , "-w")){

str4 = widen_stars(str1 , str2 );

printf ("the combined string is : %s",str4);

}else {

printf(" please input the right option");

}

free(str3);

str3=NULL;

free(str4);

str4=NULL;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote