Write an ARM function that will insert a second string into a first string at a
ID: 644097 • Letter: W
Question
Write an ARM function that will insert a second string into a first string at a specific offset and return a pointer to the new string. The C language driver code is:
#include <stdio.h>
#include <stdlib.h>
extern char * ins( char * string1, char * string2, int place ) ;
int main( int argc, char * argv[] )
{
char s1[] = "String to insert into" ;
char s2[] = " this is the string to insert" ;
int place = 9 ;
char * result ;
result = ins( s1, s2, place ) ;
printf( "s1: %s ", s1 ) ;
printf( "s2: %s ", s2 ) ;
printf( "resulting string: %s ", result ) ;
exit( 0 ) ;
}
Use the library routine malloc to obtain space for the new string. mallocs input is a number of bytes in a1 and it returns a pointer to the obtained space in a1.
a1 is the calculated number of bytes
bl malloc library routine
a1 is a pointer to the obtained space
Explanation / Answer
char* ins(char* s1, char* s2,int pos){
int i,j,count=0;
char* res;
char *resop=(char*)malloc((strlen(s2)+pos+1)*sizeof(char));
int num=strlen(s2)+pos+1;
for(j=0;j<pos;j++)
{
*(resop+j)=*(s1+j);
count++;
}
for(i=0;i<strlen(s2);i++)
{
*(resop+count)=*(s2+i);
count++;
}
res=resop;
return res;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.