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

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

ID: 3917192 • Letter: P

Question

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

Explanation / Answer

Part 1:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 30

/*
Function to combine alternatively placing one character at a time from each string.
Parameters:
stringOne -> First string
stringTwo -> Second string
combinedString -> To store the result string
*/
void combineI(const char *stringOne, const char *stringTwo, char *combinedString)
{
// Loops till end of first or second string
while (*stringOne != '' && *stringTwo != '')
{
// Stores the current character from first string in result string
// And increase both the string pointing position by one
*combinedString++ = *stringOne++;

// Stores the current character from second string in result string
// And increase both the string pointing position by one
*combinedString++ = *stringTwo++;
}// End of while loop

// To store remaining characters of string one
// Loops till end of the first string
while (*stringOne != '')
// Stores the current character from first string in result string
// And increase both the string pointing position by one
*combinedString++ = *stringOne++;

// To store remaining characters of string two
// Loops till end of the second string
while (*stringTwo != '')
// Stores the current character from second string in result string
// And increase both the string pointing position by one
*combinedString++ = *stringTwo++;
// Assigns null character at the end
*combinedString = '';
}// End of function

/*
Function to combine two strings and '*' character inserted in between every character in those strings except at the end
Parameters:
stringOne -> First string
stringTwo -> Second string
combinedString -> To store the result string
*/
void combineW(const char *stringOne, const char *stringTwo, char *combinedString)
{
char *symbol = "*";

// Loops till end of the first string
while (*stringOne != '')
{
// Stores the current character from first string in result string
// And increase both the string pointing position by one
*combinedString++ = *stringOne++;
// Stores the '*' symbol and increase the pointing position by one
*combinedString++ = *symbol;
}// End of while loop

// Loops till end of the second string
while (*stringTwo != '')
{
// Stores the current character from second string in result string
// And increase both the string pointing position by one
*combinedString++ = *stringTwo++;

// Checks if string length is not zero
if(strlen(stringTwo))
// Stores the '*' symbol and increase the pointing position by one
*combinedString++ = *symbol;
}// End of while loop
// Assigns null character at the end
*combinedString = '';
}// End of function

// main function definition
int main(int argc, char** argv)
{
// Declares two arrays of size MAX to store tow strings
char one[MAX], two[MAX];
// Checks if number of arguments is less than 3 then display error message
if(argc < 3)
{
printf(" Invalid arguments %d.", argc);
exit(0);
}// End of if condition

// Accepts two strings from the user
printf(" Please enter a string of maximum 30 characters: ");
scanf("%30s", one);
fflush(stdin);
printf(" Please enter a string of maximum 30 characters: ");
scanf("%30s", two);

// Checks if second argument is "-i"
if(strcmpi(argv[2], "-i") == 0)
{
// Dynamically allocates memory by with string one plus string two plus one size
char *combinedString = malloc(strlen(one) + strlen(two) + 1);
// Calls the function to combine the strings
combineI(one,two, combinedString);
// Displays the result
printf("The combined string is: %s", combinedString);
}// End of if condition

// Otherwise, checks if second argument is "-w"
else if(strcmpi(argv[2], "-w") == 0)
{
// Dynamically allocates memory by with string one plus string two plus both string size plus one size
char *combinedString = malloc(strlen(one) + strlen(two) + (strlen(one) + strlen(two)) + 1);
// Calls the function to combine both the strings with '*'
combineW(one,two, combinedString);
// Displays the result
printf("The combined string is: %s", combinedString);
}// End of else if condition
// Otherwise display error message invalid command
else
printf("Invalid command.");
return 0;
}// End of main function

Sample Output 1:

Please enter a string of maximum 30 characters: Commander

Please enter a string of maximum 30 characters: 123
The combined string is: C1o2m3mander

Sample Output 2:

Please enter a string of maximum 30 characters: check

Please enter a string of maximum 30 characters: 1234567
The combined string is: c*h*e*c*k*1*2*3*4*5*6*7

Part 2:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Function to display menu and returns user choice
int menu()
{
// To store user choice
int choice;
// Displays menu
printf(" Options: ");
printf(" 1 - Find string length.");
printf(" 2 - Compare strings.");
printf(" 3 - Copy strings.");
printf(" 4 - Concatenate strings.");
printf(" 5 - Quit.");
// Accepts user choice
printf(" Please enter your option: ");
scanf("%d", &choice);
// Returns user choice
return choice;
}// End of function

// Function to display strings
void displayString(char **stringData, int len)
{
int i;
printf(" Your strings are: ");
// Loops till number of strings
for ( i = 0; i < len; i++ )
printf(" String number %d - "%s" ", (i+1), stringData[i]);
}// End of function

// main function definition
int main(int argc, char** argv)
{
int i, size, firstNo, secondNo;
// Dynamically allocates memory for number of strings passed as command line argument
char **stringData = (char**) calloc(atoi(argv[1]), sizeof(char*));

// Loops till number of strings passed as command line argument
for ( i = 0; i < atoi(argv[1]); i++ )
{
// Accepts data from the user
printf(" Enter the length of string %d: ", (i+1));
scanf("%d", &size);
stringData[i] = (char*) calloc(size, sizeof(char));
printf(" Please enter string %d: ", (i+1));
scanf("%s", stringData[i]);
fflush(stdin);
}// End of for loop

// Loops till user choice is not 5
do
{
// Calls the function to display the strings
displayString(stringData, atoi(argv[1]));

// Calls the function to accept user choice and calls appropriate function
switch(menu())
{
case 1:
printf(" Enter a string number: ");
scanf("%d", &firstNo);
printf(" The length of string %d is: %d", firstNo, strlen(stringData[firstNo-1]));
break;
case 2:
printf(" Enter the number of the first string: ");
scanf("%d", &firstNo);
printf(" Enter the number of the second string: ");
scanf("%d", &secondNo);
i = strcasecmp(stringData[secondNo-1], stringData[firstNo-1]);
if(i == 0)
printf(" Both are equal.");
else if(i > 0)
printf(" First string is greater than second string.");
else
printf(" First string is less than second string.");
break;
case 3:
printf(" Enter the number of the source string: ");
scanf("%d", &firstNo);
printf(" Enter the number of the destination string: ");
scanf("%d", &secondNo);
strcpy(stringData[secondNo-1], stringData[firstNo-1]);
printf(" After copy source string %s destination string %s: ", stringData[firstNo-1], stringData[secondNo-1]);
break;
case 4:
printf(" Enter the number of the source string: ");
scanf("%d", &firstNo);
printf(" Enter the number of the destination string: ");
scanf("%d", &secondNo);
strcat(stringData[secondNo-1], stringData[firstNo-1]);
printf(" After Concatenation source string %s destination string %s: ", stringData[firstNo-1], stringData[secondNo-1]);
break;
case 5:
exit(0);
default:
printf(" Invalid Option.");
}// End of switch - case
}while(1); // End of do - while loop
return 0;
}// End of main function

Sample Output:

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: 4

The length of string 4 is: 6
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: 2

Enter the number of the second string: 3

First string is greater than second string.
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: 3

After copy source string second destination string second:
Your strings are:
String number 1 - "first"
String number 2 - "second"
String number 3 - "second"
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: 4

Enter the number of the source string: 1

Enter the number of the destination string: 5

After Concatenation source string first destination string fifthfirst:
Your strings are:
String number 1 - "first"
String number 2 - "second"
String number 3 - "second"
String number 4 - "fourth"
String number 5 - "fifthfirst"
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: 2

Enter the number of the second string: 3

Both are equal.
Your strings are:
String number 1 - "first"
String number 2 - "second"
String number 3 - "second"
String number 4 - "fourth"
String number 5 - "fifthfirst"
Options:
1 - Find string length.
2 - Compare strings.
3 - Copy strings.
4 - Concatenate strings.
5 - Quit.
Please enter your option: 5

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