Google Answers Logo
View Question
 
Q: writing a function similar to strcat function...assignment. Need help ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: writing a function similar to strcat function...assignment. Need help
Category: Computers > Programming
Asked by: derekwtp-ga
List Price: $10.00
Posted: 25 Feb 2003 18:16 PST
Expires: 27 Mar 2003 18:16 PST
Question ID: 167159
here is main ( just using main as a driver right now just to get function working)

main()
 {
 void removestring(char*,int,int);
 int findstring(char*,char*);
 void insertstring(char*,char*,int);
 /*void replacestring(char*,char*,char*);*/
 printf ("main\n");

 char beacon[80];
 char bread[80];
 char text[80];
 char s1[80];
 char s2[80];
 int x;
 
 strcpy(text," one for all and all for one");
 strcpy(beacon,"Derek Winchester");
 strcpy(bread," Steven");
 strcpy(s1, "one");
 strcpy(s2, "two");

 
 insertstring(beacon,bread,6);
 
 
 

 }
 


here is the function

void insertstring(char *first,char *second,int number)
{
    
char *tempfirst= first;
char *tempsecond= second;
int tempnum = strlen(second);
char *temp= first + number;
int i;
printf ("%s\n",temp);

*tempfirst = *first + number;

while ((*(tempfirst++) = *(tempsecond++)) != '\0');
while ((*(tempfirst++) = *(temp++)) != '\0');
*(tempfirst) = '\0';


 

I am stuck  trying to get insertstring function to work. please help
Answer  
Subject: Re: writing a function similar to strcat function...assignment. Need help
Answered By: maniac-ga on 25 Feb 2003 20:02 PST
Rated:4 out of 5 stars
 
Hello Derekwtp,

I will assume that "insertstring" is supposed to insert the second
string into the first at the offset specified by number. For your
example, the result would be
  Derek Steven Winchester
The following works on that assumption - use a clarification request
if this is incorrect.

The main problem is that you need to move the characters after the
insertion location out of the way before doing the insert. With your
example, something like...
  Derek Winchester
move the end of the string " Winchester" out of the way
  Derek Winche Winchester
and then copy the new string into the middle
  Derek Steven Winchester
to get the proper result.

Important - copy the characters " Winchester" in reverse order since
the source and destination areas overlap. Using bcopy (3) would work
or implement a loop that moves the characters in that order (e.g.,
using -- for your pointers).

To do this, you need...
 - the string length of first (firstlen)
 - the string length of second (secondlen)
Two calls to bcopy would do the job; something like...
  bcopy (first+number-1, first+number-1+secondlen, 2+firstlen-number);
  bcopy (second, first+number-1, secondlen);
Or implement two loops to do the proper copies. Note that C uses zero
based arrays and pointers so the sixth character (the space) is at
first+5, not first+6. Also note that you must add two to the first
bcopy to get the null character copied (the 17th character - a total
of 12 are moved, leaving five in the original position). You don't add
one to the second bcopy because you just want the seven characters
moved (and not the null). [This is why I made the third general
comment below]

A few general comments on your code that may help in your efforts:
 - be sure to #include all the appropriate system header files. In
this case
 #include <string.h>
 #include <stdio.h>
to prevent problems with function prototypes or other possible
problems calling the system functions.
 - if you use gcc, use -Wall [or whatever your compiler uses for "all
warnings"] to point out possible coding problems. gcc generated six
warnings even after adding those header files, mostly unused
variables.
 - step through or draw out the steps to be performed; then write the
code to implement those. Use the debugger (or printf calls) to confirm
proper operation of each step.

Good luck on your work and don't hesitate to ask for a clarification
if this does not make sense.
  --Maniac

Request for Answer Clarification by derekwtp-ga on 26 Feb 2003 07:18 PST
Thanks maniac, the problem is that I can't use bcopy which makes this
difficult. How would I get the same effect w/out using bcopy?

Clarification of Answer by maniac-ga on 26 Feb 2003 09:46 PST
Hello Derekwtp,

As I mentioned before
   ... or implement a loop that moves the characters in that order ...
I assumed this was a homework assignment and that you wanted a
description of *how* to do this, not to give you a specific
implementation (that you might not be able to turn in as *your work*).
If that is not the case, please indicate in a clarification request.

To give you a specific implementation and using a recent version of
glibc as a reference
  ftp://ftp.gnu.org/pub/gnu/glibc/glibc-2.3.tar.gz
you can download & extract the tar file with a command like
  tar xzf glibc-2.3.tar.gz
and looking at sysdeps/generic/memcpy.h there are macros named
BYTE_COPY_FWD and BYTE_COPY_BWD which implement the forward and
backwards byte copies with very similar looking loops. I'll quote the
BYTE_COPY_BWD macro as an example. Note that src_ep and dst_ep are at
the address *after* the range to be moved.

#define BYTE_COPY_BWD(dst_ep, src_ep, nbytes) \
  do \
    { \
      size_t __nbytes = (nbytes); \
      while (__nbytes > 0) \
        {\
          byte __x; \
          src_ep -= 1; \
          __x = ((byte *) src_ep)[0]; \
          dst_ep -= 1; \
          __nbytes -= 1; \
          ((byte *) dst_ep)[0] = __x; \
        } \
    } while (0)

Note this is code that tries to be *very careful* to ensure the values
copied are of the correct data type, that nbytes is not modified
(though both dst_ep and src_ep are modified), and exactly what needs
to be moved is moved. If you use this algorithm as-is, you should
credit the source. Your loop can be a lot simpler since the data types
are constrained and you already have variables assigned for
intermediate values.

In the actual implementation of bcopy, there is a lot of code to
determine the direction of the copy and optimize the copy operation.
This is the macro used to do a short copy or "finish" a long copy that
is not quite aligned right. It should work properly (but perhaps not
as fast) in all cases where the backwards copy is needed. As a side
note - on some systems, a loop like this is *very slow* compared to a
properly aligned forward copy due to the effects of system caches.

  --Maniac

Request for Answer Clarification by derekwtp-ga on 26 Feb 2003 09:49 PST
Maniac, 
 this is what i came up w/ but it still needs some tweaking. Do you
know how I could get spacing in this w/out altering the input string.
example:

" Winchester"
" Steven"

this is how i am declaring the string. how can i add the spaces via
function? I cant just adjust character placement, because then the
rest of the second part of the string that isnt overwritten will be
shown.


void insertstring(char *first,char *second,int number)
{

char temp;
char string;
int secondlen = strlen(second);
int firstlen = strlen(first);
int maxlen= firstlen - number;
int together= firstlen + secondlen;
int i;

       printf ("%i\n",maxlen);


/*for (i=1;i<=15;i++)*/
do
{ temp =*(first + firstlen) ;
  *(first+(together))=temp;
  
  

/*printf ("%c temp\n",temp);
printf ("%i firstlen\n",firstlen);
printf ("%i together\n",together);
printf ("%s first\n",first);  */
firstlen--;
together--;
maxlen--;
/*temp = 0; */
}
while (maxlen >= 0);


do
{
string = *second;
*(first+number) = string;

number++;
*second++;

 }
while (*second != '\0');





printf ("%s first\n",first);
printf ("%s second\n",second);




}

Clarification of Answer by maniac-ga on 26 Feb 2003 16:04 PST
Hello Derekwtp,

The code you show is "close" but not quite the right answer. Let me
use an illustration and then comment on the loops in the code to show
where they are not quite correct. The numbers below each letter
represent the offset from "first" and "second". So first+6 refers to
the W, not the space (the sixth character).

Derek Winchester
00000000001111111111
01234567890123456789

 Stephen
0000000000
0123456789

The first loop (lines 19-34) stops one character too soon. The first
example using bcopy used
  2+firstlen-number
for a reason; this is the number of characters to move and the loop as
coded only moves 1+firstlen-number values.

The second loop (lines 37-46) moves the right number of characters,
but is off by one in the destination. The space in " Stephen" must go
into position first+number-1, not first+number. That matches the value
in the second bcopy in my original answer.

I solved this by drawing out the example shown above, and examining
the values in each equation. You can do this by hand or by using a
debugger (e.g., gdb). You may find it necessary to do this in your
future work to prevent this type of "one off" problem.

  --Maniac
derekwtp-ga rated this answer:4 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy