Question:
Concatenating String using pointers PLEASE HELP?
Aida
2012-01-28 10:33:14 UTC
I'm trying to write this code to concatenate and print two strings but I'm stuck. I must use pointers. Don't know why my code won't work.

char *concat(char *a, char *b){
int length;
for (length = 0; *a !='\0'; *a++){
length++;
}
int len;
for (len = 0; *b != '\0'; *b++){
len++;
}

char *cc = malloc((length+len+1)*sizeof(char));
char *c = *cc;
int i;
for (i = 0; i< length; i++){
*(c+i) = *(a+i);
}
for (i = 0; i< len; i++){
*(c+i) = *(b+i);
}
*c = '\0';
*c = *cc;
printf("%s\n", *c);
Three answers:
McFate
2012-01-28 10:44:39 UTC
It doesn't work for two reasons:



(1) because you copy a over to *c, but then copy b over to *c again. Since c is not advanced, you are writing a and b over top of each other, instead of consecutively. And then you set *c = '\0', overwriting b with a zero-length string.



(2) because you aren't providing a String to printf(). The argument matching a "%s" format is a char * (e.g., "cc"). What you are supplying is a char (*cc).



To solve the first issue, I would increment c every time and just set *(c++) = a[i], then *(c++) = b[i], and finally *c = '\0'.



This:

=========

for (i = 0; i< length; i++) *(c+i) = *(a+i);

for (i = 0; i< len; i++) *(c+i) = *(b+i);

*c = '\0';

=======



Becomes:

=======

for (i = 0; i< length; i++) *(c++) = a[i];

for (i = 0; i< len; i++) *(c++) = b[i];

*c = '\0';

========



Now you have the two strings concatenated, and cc still points to the start of the two. Now you can use cc to print your String:



printf("%s\n", cc);



@M
Michael
2012-01-28 19:14:37 UTC
There are a couple of big things wrong - when you calculate the length of the two strings you are incrementing the pointers a and b but you didn't save a copy of their original values so when you come to do the copy and concatenation all you have are pointers to two empty strings.



As mentioned in the first answer you are passing a single character to printf instead of a pointer.



Also although you are "using pointers" in several places you are using them as if they were arrays *(c+i) etc - while that is perfectly correct it probably isn't really what was wanted.



Something like this would be better:



char *concat(const char *a, const char *b)

{

char *result = malloc(strlen(a) + strlen(b) + 1);



if (result) {

char * r = result;



while (*a)

*r++ = *a++;



while (*b)

*r++ = *b++;



*r = '\0';

}



return result;

}



int main()

{

printf("%s\n", concat("hello ", "world"));

}



If you aren't allowed to use strlen() then write your own version of strlen() and call it - don't just do it as an inline calculation
roger
2012-01-28 19:12:43 UTC
here is one

the destination string must be big enuf to hold both strings

untested code follows:



#include

#include

char *concat(char *, char *);

int main(void){

char *c,*s,*d;

s="Hello ";

d="World";

c=concat(d,s);



printf("%s\n",c);

return 0;

}





char *concat(char *d, char *s){

char * s1,*s2;

s1=malloc(strlen(d) + strlen(s) +1); // sizeof(char) is defined to be 1

s2=s1; // save start

// start copying

while(*s1++=*d++); // copy first string into s1 -- note the single = we copy 'til we get to a zero byte

s1--; // so we overwrite the 0 byte at the end of s1

while(*s1++=*s++); // copy second string

return s2;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...