COULD ANY ONEHELP ME WRITING THIS CODE WITH \"C PROGRAMMING\".HOWEVER,I DO NOT U
ID: 3636525 • Letter: C
Question
COULD ANY ONEHELP ME WRITING THIS CODE WITH "C PROGRAMMING".HOWEVER,I DO NOT UNDERSTAND COMPLICATED CODE SO WHO WRITE THIS CODE,COULD IT BE WRITTEN IN SIMPLE WAY...THANK YOUThe functions that you must define are listed below. All of the character manipulation done by your functions must be done using the arrays passed into the functions as arguments. You may not define any additional arrays in your functions or globally.
For all of these descriptions, the arguments s, s1, s2 are C-strings. The functions, reverse() and insert(), alter their first argument. The insert function does not check for overflow of the array of the first argument.
Prototype:int length(char s[]);
Description:Returns the length of s, which is the number of characters in s, not including the terminating null character. The null string would have a length of 0.
Prototype:void reverse(char s[]);
Description:Reverses the order of the characters in the string s.
Prototype:void insert(char s1[],char s2[], int n);
Description:Inserts the string s2 in the string s1 immediately following the nth character in s1. If n is 0, s2 is inserted before the first character in s1. If n is greater than the number of characters in s1, s2 is inserted after the last character in s1.
Prototype:int search(char s1[],char s2[]);
Description:Searches for the string s2 in the string s1. If s2 is a substring of s1, true is returned (i.e., return 1;). Otherwise, false is returned (i.e., return 0;). The null string is always a substring of any other string.
Here are some examples of how each function can be used. Assume the string variables a, b, c, d, and e are defined like this.
#define MAX_STR_LEN = 99;
char a[MAX_STR_LEN 1] = "Hello"; //Note: These = signs represent
char b[MAX_STR_LEN 1] = "Goodbye"; //initializations, not
char c[MAX_STR_LEN 1] = "1234"; // assignments,
char d[MAX_STR_LEN 1] = "1236789"; //and are therefore legal.
char e[MAX_STR_LEN 1] = "CSC";
Function call:length(a)
Result or return value:5
Function call:reverse(a);
Result or return value:a becomes "olleH". a[5] remains ''.
Function call:insert(b, " Bob", 20);
Result or return value:b becomes "Goodbye Bob". b[11] = ''.
Function call:insert(c, "0", 0)
Result or return value:c becomes "01234". c[5] = ''.
Function call:insert(d, "45", 3);
Result or return value:d becomes "123456789". d[9] = ''.
Function call:insert(e, "210", 3);
Result or return value:e becomes "CSC210". e[6] = ''.
Function call:search(d, "123")
Result or return value:true
Function call:search("abcabcdabcde", "bcd")
Result or return value:true
Function call:search("abcabcdabcde", "xy")
Result or return value:false
Explanation / Answer
please rate - thanks
message me before rating if any problems-thanks
//my compiler did not allow a[MAX_STR_LEN 1]
//change all 100 to MAX_STR_LEN+1
//change all 99 to MAX_STR_LEN
//this construct is allowed in C++ not C as far as I know
if your compiler doesn't like
#include <conio.h>
and
getch();
just remove them. I need them to keep my DOS window open using DEV C++
#include <stdio.h>
#include <conio.h>
#define MAX_STR_LEN = 99;
int length(char s[]);
void reverse(char s[]);
void insert(char s1[],char s2[], int n);
int search(char s1[],char s2[]);
int main()
{char a[100] = "Hello"; //Note: These = signs represent
char b[100] = "Goodbye"; //initializations, not
char c[100] = "1234"; // assignments,
char d[100] = "1236789"; //and are therefore legal.
char e[100] = "CSC";
printf("The length of %s = %d ",a,length(a));
printf("%s reversed = ",a);
reverse(a);
printf("%s ",a);
insert(b, " Bob", 20);
printf("results of insert(b, " Bob", 20); is %s ",b);
insert(c, "0", 0) ;
printf("results of insert(c, "0", 0); is %s ",c);
insert(d, "45", 3);
printf("results of insert(d, "45", 3); is %s ",d);
insert(e, "210", 3);
printf("results of insert(e, "210", 3); is %s ",e);
printf("results of search(d, "123") is ");
if( search(d, "123") ==1)
printf("true ");
else
printf("false ");
printf("results of search("abcabcdabcde", "bcd") is ");
if( search("abcabcdabcde", "bcd") ==1)
printf("true ");
else
printf("false ");
printf("results of search("abcabcdabcde", "xy") is ");
if( search("abcabcdabcde", "xy") ==1)
printf("true ");
else
printf("false ");
getch();
return 0;
}
int length(char s[])
{int i;
for(i=0;i<100;i++)
if(s[i]=='')
return i;
return 100;
}
void reverse(char s[])
{int i;
char temp;
int n=length(s);
for(i=0;i<n/2;i++)
{temp=s[i];
s[i]=s[n-1-i];
s[n-1-i]=temp;
}
}
void insert(char s1[],char s2[], int n)
{int i,l1=length(s1),l2=length(s2);
if(n==0)
{for(i=l1+1;i>0;i--)
s1[i]=s1[i-1];
}
else if(n>l1)
{n=l1;
}
else
{for(i=l1+l2;i>n;i--)
s1[i]=s1[i-l2];
}
for(i=0;i<l2;i++)
s1[n+i]=s2[i];
s1[l1+l2]='';
}
int search(char s1[],char s2[])
{int i,j,l1=length(s1),l2=length(s2);
if(l2==0)
return 1;
if(l2>l1)
return 0;
for(j=0;j<l1-l2;j++)
{for(i=0;i<l2;i++)
{if(s1[j+i]!=s2[i])
i=l2+5; //force exit of inner loop
}
if(i==l2)
return 1;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.