Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need the function of strcat using pointers??

Status
Not open for further replies.

Cina

Programmer
Feb 2, 2001
2
US
I have to write the C function of
strcat using pointers and I can't
seem to find it anywhere.
Please help with comments.
 
be a little more specific? I assume it takes 2 args, but are they pointers to characters? Or should the function be a pointer to a function? If the args are char *, what are they terminated with, and what does the function return?

That will help.
MWB As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
I need to write the function for strcat without using <string.h>. It doesn't need to return anything but it needs to use pointers to characters.

 

void mystrcat(char * string1 , char * string2)
{
while(*string1)
string1++; /* go to the end of string1*/

/* copy string 2 at the end of string 1*/

while(*string2)
*(string1++) = *(string2 ++) ;

}



 
hey jude,
your function works. but i think you should tell cina how dangerous it is.
bye
noka
 
Hello Jude,
I tried your srtcat function. It compiles fine but creates problems during runtime. With two strings (&quot;strings&quot; and &quot;have been concatenated&quot;), I got the following.

&quot; strings have been concatenated¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ 8_e &quot;.

If I add a third string I get a runtime failure on the screem.
Does anyone know how to fix this ??? I am trying this exercise myself and haven't solved it yet.
Thanks,
-John
 
hi john,

this sounds like a class assignment. and so take a look at the size of ur destination string. is it large enough to receive the total data? a &quot;string&quot; in C must be terminated with a null &quot;\0&quot; or 0 (ascii val) character.. is that correct for ur strings??

and btw at the end of the second while loop in jude's function u might want to add *string1 = 0; to terminate the string..

hth,
shail

 
Shail is right. In Jude's code, in last line there is no '\0' character to close the string so the output is incorrect. Second, the algorithm is right, but in implementation using C, it's NOT SAFE because we store data beyond string1 and that could overwrite some information or other variable data.

#include <alloc.h>
// ...

void mySecondStrCat(char * str1, char * str2)
{
int str1Len = 0;
int str2Len = 0;

while ( str1++ ) str1Len++; // get the length of str1.
while ( str2++ ) str2Len++; // get the length of str2.

// create a temporary variable to hold the old string
// of str1.
char *temp = (char *) malloc(str1Len + 1);

// move back the str1 to the first character (b'cuz we
// have moved it to count the str1Len) and while moving
// backwards, copy it to temp.
for (register int i = str1Len - 1; i >=0 ; i--) {
temp = str1;
temp--; str1--;
}
temp[str1Len] = '\0';

free( str1 ); // clean the str1 contents (although you
// didn't allocate it using malloc, it's
// okay :).

// allocate the enough space for concattenating str1
// with str2.
str1 = (char *) malloc(str1Len + str2Len + 1);

// move back str2 to the first character (b'cuz we also
// moved it to the last character for counting str2Len)
for (i = str2Len - 1; i >= 0; i--)
str2--;

// Now copy the temp to str1 and concat it
// with str2.
int j = 0;
for (i = 0, j = 0; i < str1Len; i++, j++)
str1[j] = temp;

for (i = 0; i < str2Len; i++, j++)
str1[j] = str2;

str1[j] = '\0'; // close the string.
}

/**
* Note: I haven't try to run this function yet, but I think
* thats the safer algorithm for the process.
* Maybe someone else can give more better solution.
*/
 
void main ()
{
int i = 0;
char a[50], b[25];

printf (&quot;Enter first string>&quot;);
while ((a[i++] = getchar() )!= '\n');
printf (&quot;Enter first string>&quot;);
gets(b);

for (int j = 0; b[j] != '\0'; j++, a[i+j] = b[j]);
a[i + j] = '\0';

printf (&quot;a + b = %s&quot;, a);
} Andrew Lim

Trying and exploring is adventuring
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top