Write a function named append that accepts three arguments. The first two argume
ID: 3549629 • Letter: W
Question
Write a function named append that accepts three arguments. The first two arguments passed to append are c-strings to be appended (the second c-string is appended to the first). The third argument passed should be the size of the character array passed as the first argument.
Before appending, append should verify there is enough space in the character array to accept the appended string. If there is, then the append operation should proceed and append should return the total size of the appended string. If the appended string is too large to fit inside the character array, then append should return -1 without appending.
Demonstrate the function in a program.
Hint: You may find the c-string functions strlen() and strcat() useful.
In other words, write a function that verifies there is enough space to append a c-string to an existing c-string. If there is, append and return the size of the appended string. If not, return -1.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
int append(char *,char *,int);
void main()
{
clrscr();
char a[50],b[20];
int r,l;
cout<<"enter a string";
gets(a);
cout<<" enter another string";
gets(b);
l=strlen(a);
r=(a,b,l);
if(r==-1)
cout<<" the second string cannot be appended to the first string";
else
cout<<" the size after appending="<<r;
getch();
}
int append(char *s1,char *s2,int l1)
{
int l2;
l2=strlen(s2);
if((l1-l2)>=l2)
return (l1+l2);
else
return -1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.