Question

Please note that I cannot use the string library function and that it must be coded...

Please note that I cannot use the string library function and that it must be coded in C89, thank you!

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 prohibite

d. Points will be deducted if goto is used.

Part A

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

Please enter a string of maximum 30 characters:

1234567

The combined string is: a1b2c3d4e567

Or

> combine_strings -w

Please enter a string of maximum 30 characters:

abcde

Please enter a string of maximum 30 characters:

1234567

The combined string is: a*b*c*d*e*1*2*3*4*5*6*7

Notes:

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

• Before terminating, your program needs to de

-allocate the space allocated for the newly created strings

Save your program as

combine_strings.c

Part 2

This program will build off the last project. You will write an interactive program for strings. The

number of strings will be provided as a command line argument. The user will enter the length of the string they are about to enter and then the string itself. After the program has read in the specified number of strings it will print the strings out and then print a menu that will allow the user to either: find the length of any string they want, compare any two strings, copy any string into another, or copy any string into another. At every iteration, your program should:

1.Print out the strings by first printing

“Your strings are:” and then the strings in the format

specified below

2. Print out a menu as follows:

Options:

1 – Find string length

2 – Compare strings

3 – Copy strings

4 – Concatenate strings

5 – Quit

Please enter your option:

Below is a description of what the program should do for each of the above options. (items underlined are to be entered by the user):

1. Find the length of any string. You will use the strlen function. For this option, the program should ask the user to enter the number of the string they want to find the length of, as follows:

Enter a string number: 1

The length of string 1 is: 5

2. Compare any two strings.

You will use the strcmp function.

For this option, the program should ask the user to enter the numbers of two strings and then print out which string comes first, as follows:

Enter the number of the first string: 4

Enter the number of the second string: 2

String 2 comes before string 4 alphabetically.

If the strings selected by the user are the same string, your program should output: “The two strings are the same.”

3.Copy any two strings. You will use the strcpy function. For this option, the program should ask the user

to enter the numbers of two strings and then copy the source string into the destination string, as follows:

Enter the number of the source string: 3

Enter the number of the destination string: 6

4. Concatenate any two strings. Your function will use the strcat function. For this option, your program should ask the user to enter the numbers of two strings and then add the source string to the end of the

destination string, as follows:

Enter the number of the source string: 3

Enter the number of the destination string: 6

5.Exit the program. Your program should free any memory that was allocated before exiting.

Your program should use the four functions written in the last project but modified as necessary:

1. strlen: This function will take as a parameter a character

pointer to a string. It will return the length of

the string, not including the null terminator. This function is exactly the same as the last project.

2. strcpy: This function should take as parameters two character pointers to two strings (a source string and a destination string). First, the function must reallocate enough memory to hold the source string in the destination string. Then it will copy the source string into the destination string, including the null terminator. It will then return a pointer to the beginning of the destination string.

3. strcat: This function should take as parameters two character pointers to two strings (a source string and a destination string). First, the function should reallocate enough memory to add the source string to the end of the destination string. Then it will add the source string to the end of the destination string. It will then return a pointer to the beginning of the destination string.

4. strcmp: This function will take as parameters two character pointers to two strings (string 1 and string 2). The function then compares the two strings and if the string 1 comes first alphabetically it returns 1. If the string 2 comes first alphabetically then it

returns -1. If the strings are the same then the function returns 0. The function should compare the two strings one character at a time and return the appropriate value as soon as a difference is noticed between the strings. This function is exactly the same as the last project.

The program should function as follows (items underlined are to be entered by the user):

> dynamic_strings 5

Enter the length of string 1: 5

Please enter string 1: first

Enter the length of string 2: 6

Please enter string 2: second

Enter the length of string 3: 5

Please enter string 3: third

Enter the length of string 4: 6

Please enter string 4: fourth

Enter the length of string 5: 5

Please enter string 5: fifth

Your strings are:

String number 1 – “first”

String number 2 – “second”

String number 3 – “third”

String number 4 – “fourth”

String number 5 – “fifth”

Options:

1 – Find string length

2 – Compare strings

3 – Copy strings

4 – Concatenate strings

5 – Quit

Please enter your option: 1

Enter a string number: 3

The length of string 3 is: 5

Your strings are:

String number 1 – “first”

String number 2 – “second”

String number 3 – “third”

String number 4 – “fourth”

String number 5 – “fifth”

Options:

1 – Find string length

2 – Compare strings

3 – Copy strings

4 – Concatenate strings

5 - Quit

Please enter your option: 2

Enter the number of the first string: 3

Enter the number of the second string: 4

String 4 comes before string 3 alphabetically.

Your strings are:

String number 1 – “first”

String number 2 – “second”

String number 3 – “third”

String number 4 -“fourth”

String number 5 – “fifth”

Options:

1 – Find string length

2 – Compare strings

3 – Copy strings

4 – Concatenate strings

5 – Quit

Please enter your option: 3

Enter the number of the source string: 2

Enter the number of the destination string: 5

Your strings are:

String number 1 – “first”

String number 2 – “second”

String number 3 – “third”

String number 4 - “fourth”

String number 5 – “second”

Options:

1 – Find string length

2 – Compare strings

3 – Copy strings

4 – Concatenate strings

5 – Quit

Please enter your option: 4

Enter the number of the source string: 4

Enter the number of the destination string: 1

Your strings are:

String number 1 – “firstfourth”

String number 2 –“second”

String number 3 – “third”

String number 4 –“fourth”

String number 5 – “second”

Options:

1 - Find string length

2 – Compare strings

3 – Copy strings

4 – Concatenate strings

5 – Quit

Please enter your option: 5

Notes:

•If the user enters an invalid menu option, the program should print “Invalid Option.” then it should print the menu again and ask for a valid option, as follows:

Invalid Option.

Your strings are:

String number 1 – “first”

String number 2 – “second”

String number 3 – “third”

String number 4 – “fourth”

String number 5 – “fifth”

Options:

1 – Find string length

2 – Compare strings

3 – Copy strings

4 - Concatenate strings

5 – Quit

Please enter your option:

•You may NOT include the string library. Inclusion of the string library will result in a

grade of zero for this project. Use the functions from project 7 and make any necessary changes.

•In the last project you used static memory allocation and assumed a max string size of 50 characters. This is no longer the case. You must dynamically allocate memory for all your strings and it is your responsibility to free the memory before the program ends.

•For this program, you must take the number of strings in as a command line argument. This number will be

stored as a string not as an integer. To get the integer value of a string use the atoi(argv[1])function from stdlib. This function takes as a parameter a string and gives the

equivalent integer value.

Save your program as

dynamic_strings.c

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

Part A

code in c in visual studio

#include<stdio.h>//for stadndard input ouptut

#include<stdlib.h>//for malloc

char* intersperse(char[],char[]);//function prototype

char* widen_stars(char[],char[]);

int strcmp1(char [],char []);

int main(int argc, char *argv[])//main functio that recives the command arrgument in the argv array

{

char str1[30],str2[30];//str1 and str2 character array for input

char *ans;//its a finale ans

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

scanf("%s",&str1);

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

scanf("%s",&str2);

if(strcmp1(argv[1],"-i")==1)//for the compare with command argumnet if its -i

{

ans=intersperse(str1,str2);//call the function with both inputed string

printf("\n\nThe combined string is::%s\n\n",ans);

}

else if(strcmp1(argv[1],"-w")==1)//for the compare with command argumnet if its -w

{

ans=widen_stars(str1,str2);//call the function with both inputed string

printf("\n\nThe combined string is::%s\n\n",ans);

}

else

printf("Invalid Argument!!!");//if argument is other then the -i and -w

return 0;

}

char* intersperse(char s1[],char s2[])//function that interspersing

{

int i,j=0,slen1=0,slen2=0,flag=0;//integer variables

char *strans;//that store the ans

for(i=0;s1[i]!=NULL;i++)//for finding the length for string 1

slen1++;

for(i=0;s2[i]!=NULL;i++)//for finding the length for string 2

slen2++;

strans = (char*)malloc((slen1+slen2)*sizeof(char));//assign the size of the strans pointer

if(slen1>slen2)//if length of the string 1 is more then then string 2

{

for(i=0;i<slen1;i++)//for loop until the string length of string 1

{

if(s2[i]!=NULL)//if string 2 character is not null

{

strans[j++]=s1[i];//then first copy the string 1 character

strans[j++]=s2[i];//then copy string 2 character

}

else//this is for when string 2 has no character left

strans[j++]=s1[i];

}

}

else if(slen2>slen1)//this is for if string 2 has more character then string 1

{

for(i=0;i<slen2;i++)

{

if(s1[i]!=NULL && flag!=1)

{

strans[j++]=s1[i];

strans[j++]=s2[i];

}

else

{

flag=1;

strans[j++]=s2[i];

}

}

}

else//this is for if both strings has eqals character

{

for(i=0;i<slen2;i++)

{

strans[j++]=s1[i];

strans[j++]=s2[i];

}

}

strans[j]='\0';

return strans;

}

char* widen_stars(char s1[],char s2[])//funciton of widen_star

{

int i,j=0,slen1=0,slen2=0,flag=0;

char *strans;

for(i=0;s1[i]!=NULL;i++)//findings lengths

slen1++;

for(i=0;s2[i]!=NULL;i++)

slen2++;

strans = (char*)malloc(((slen1*2)+(slen2*2)-1)*sizeof(char));//assigning the memory

for(i=0;s1[i]!=NULL;i++)//first copy the one character of first string and then * until the length of the string 1 to strans

{

strans[j++]=s1[i];//assining the character of string 1

strans[j++]='*';//assigning star

}

for(i=0;s2[i]!=NULL;i++)//same as first one

{

strans[j++]=s2[i];

strans[j++]='*';

}

strans[j-1]='\0';//into lasr there is * in last character so assign the null to it

return strans;

}

int strcmp1(char arg[],char command[])//string comparission function

{

int i;

for(i=0;arg[i]!=NULL;i++)

{

if(arg[i]!=command[i])

return 0;

}

return 1;

}

output

Microsoft Visual Studio Quick Launch (Ctrl+Q) FILE EDIT VIEW PROJECT BUILD DEBUG TEAM SQL TOOLS TEST ARCHITECTURE ANALYZE WINDOW HELP o ?· ?,?. Local Windows Debugger, Debug , ?- ???·? ? ? source.c crtexe.c X Solution Explorer (Global Scope) #includec stdio.h> #includec conio.h> #includec stdlib.h> char* intersperse(char[,char[) char* widen_stars(,char[]); int strcmpl(char [1,char [1) int main(int argc, char argv]) CombineString Property Pages Platfom: Active(Win32) Configuration Manager... > Common Properties v Configuration Properties Debugger to launch: Local Windows Debugger Debugging VCDirectories char str1[3e],str2[30]; char ans; printf(argv[1]); printf(Please enter a string of maximun scanf(%s,&str1); printf(Please enter a string of maximun scanf(%s,&str2); if (stremp1(argv[1],-1)--1) Command S(TargetPath) ommand Arguments Working Directory C/C++ S(ProjectDi) No Auto Linker Manifest Tool >XML Document GeneratorDebugger Type Browse Information Build Events Merge Environment SOL Debugging Yes No ans-intersperse(stri,str2); printf(\n\nThe combined string is: >Custom Build Step > Code Analysis else if (strcmpl(argv[1],-W)1) ans-widen stars (stri,str2); printf(\n\nThe combined string is: programlcombine return 0; char intersperse(hr si],char s2) int ?,j-e, slen1:0, slen2-e, flag:0; char strans; for(1:0; s1[1] !=NULL;i++) slen1++ The command line arguments to pass to the application. 100 % Error List Output INS ENG 17:24 ^49 PIN 28-07-2018if command line is -i

if you have any query reagarding code please ask me in the comment i am here for the help. thank you.

Add a comment
Know the answer?
Add Answer to:
Please note that I cannot use the string library function and that it must be coded...
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
  • use C language please Problem 5. (5 points] Use string functions. Write a program that: 1)...

    use C language please Problem 5. (5 points] Use string functions. Write a program that: 1) Ask the user to write two strings. 2) If both strings are equal, print an appropriate message then print out both strings with their length. 3) If the strings are not equal, print an appropriate message then concatenate both strings and print out the length of the concatenating strings. 4) In the main function prints both arrays.

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

  • 2. Searching a String: Write a MIPS assembly language program to do the following: Read a...

    2. Searching a String: Write a MIPS assembly language program to do the following: Read a string and store it in memory. Limit the string length to 100 characters. Then, ask the user to enter a character. Search and count the number of occurrences of the character in the string. The search is not case sensitive. Lowercase and uppercase letters should be equal. Then ask the user to enter a string of two characters. Search and count the number of...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • Write a program with a function that accepts a string as an argument and returns a...

    Write a program with a function that accepts a string as an argument and returns a copy of the string with the first character of each sentence capitalized. For instance, if the argument is “hello. my name is Joe. what is your name?” the function should return the string “Hello. My name is Joe. What is your name?” The program should let the user enter a string and then pass it to the function. The modified string should be displayed.

  • ASSIGNMENT: Create a program to do the following: BONUS: A bonus of 20 points if you create the p...

    ASSIGNMENT: Create a program to do the following: BONUS: A bonus of 20 points if you create the program in such a way that there is no limit on the number of names to be handled. 1) Read in names to sort until the user types the “enter” key as the first character of a “C-type” string (the maximum number of names is 20, there is not a maximum length to a name), using a two dimensional array of characters....

  • Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two...

    Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two strings while ignoring the case of alphabetical characters. Your program will take two strings as input from the user and use your function to compare them. Assume a maximum C-string size of 1000 characters. Make sure your code works for any input number, not just the test cases. Your code will be tested on other test cases not listed here. Please properly comment your...

  • I need this in C not C++ just C SET-151 C Programming #1 Homework: 7 String...

    I need this in C not C++ just C SET-151 C Programming #1 Homework: 7 String Basics – Death by a thousand strings.                                                                                                                                                                         Reading(s): Chapter(s) in C Primer Plus 1-7, 9, 10 please use this format // ------------------------------------------------------------------------------------------ // Name: Your name here // Class: C Programming // Abstract: Homework 1. // ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------ // Includes // ------------------------------------------------------------------------------------------ #include<stdio.h> #include<stdlib.h> // ------------------------------------------------------------------------------------------ // Constants // ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------ // Prototypes // ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------ // Name: main //...

  • using java String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    Have the function wildcard(str) read str which will contain two strings separated by a space.The first string will consist of the following sets of characters: +, *, $ and {N} which is optional.The plus (+) character represents a single alphabetic character, the ($) character represents anumber between 1-9, and asterisk (*) represents a sequence of the same character of length 3unless it is followed by {N} which represents how many characters would appear in thesequence where N will be at...

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