|
|
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 |
|
Subject:
Re: writing a function similar to strcat function...assignment. Need help
Answered By: maniac-ga on 25 Feb 2003 20:02 PST Rated: |
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 | |
| |
| |
| |
|
derekwtp-ga rated this answer: |
|
There are no comments at this time. |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |