Question
Hello does anyone know how to fix this compiler error?? I'm doing my project in C, below is my project outline and below that is
Hello does anyone know how to fix this compiler error??
I'm doing my project in C, below is my project outline and below that is my source code
PLEASE NOTE: I AM AWARE I CAN FIX USING STRING LIBRARY, BUT IT IS NOT ALLOWED FOR THIS PROJECT, ONLY STRLEN AND STRCMP FUNCTIONS
This is my problem and below that is my source code:
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: . The assignment is 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. 3. Restriction: The use of goto statements anywhere within this program is prohibited. Points will be deducted if goto is used.
Problem:
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.
Notes: You may not include the string library, but you may use the strlen and strcmp functions from the last project (string length, string compare) Before terminating, your program needs to de-allocate the space allocated for the newly created strings
Your program should function as follows (items in italics are to be entered by the user):
> gcc combine_strings.c
>./a.out -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
> gcc combine_strings.c
>./a.out -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
//THIS IS MY SOURCE CODE
#include
#include
int strlen();
char *intersperse(char first[],char second[])
{
// calculate length for newly created string
int len = strlen(first)+strlen(second);
// dynamically allocate memory for new string
char *new = (char *)malloc(len * sizeof(char));
// fill characters in even odd index until one string exhaust
int i=0,j=0,k=0;
while(i
{
if(k%2==0)
new[k]=first[i++];
else
new[k]=second[j++];
k++;
}
// check if first string is still remaining
while(i
new[k++]=first[i++];
// check if second string is still remaining
while(j
new[k++]=second[j++];
return new;
}
char *widen_stars(char first[],char second[])
{
// calculate length for newly created string
int stringLen = strlen(first)+strlen(second),len;
// there will be stringLen-1 stars
len = stringLen + (stringLen-1);
// dynamically allocate memory for new string
char *new = (char *)malloc(len * sizeof(char));
// first fill * in new string
// at odd indexes i.e; starting from 1
int i=0,j=1;
while(j
{
new[j]='*';
j=j+2;
}
// now fill characters of first string in new string
// at even indexes i.e; starting from 0
i=0;
j=0;
while(first[i]!='\0')
{
new[j]=first[i];
i++;
j=j+2;
}
// now fill characters of second string in new string
// at even indexes i.e; starting from current value of j
i=0;
while(second[i]!='\0')
{
new[j]=second[i];
i++;
j=j+2;
}
return new;
}
int main(int argc, char *argv[])
{
int strcmp();
char first[30],second[30],*new;
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",first);
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",second);
if(strcmp(argv[1],"-i")==0)
{
new = intersperse(first,second);
}else
{
new = widen_stars(first,second);
}
printf("The combined string is: %s ",new);
free(new);
return 0;
}
combine strings.c:6:5: warning: conflicting types for built-in function strlen int strlenStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started