confused with strcat() :(

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hello12
    New Member
    • Oct 2006
    • 16

    confused with strcat() :(

    hello,
    I am supposed to concatenate 2 strings (words). For this the function should call malloc or calloc to get memory for the newstring(after concatenated) .I am not allowed to use the library string.h. I figured out to find the length of the 2 strings. .. but i don't get how to concatenate the two words.
    P.S This is the first time i am doing program with dynamic memory allocation.

    so far this is my code:
    Code:
     #include<stdio.h> 
    #include<stdlib.h>
     
     
    int my_strlen(char *string1, char *string2);
    //char*my_strcat(char *string1,char *string2);
     
    main(){
    	char *string1 = "Hello";
    	char *string2 = "world!";
     
    	my_strlen(string1,string2);
     
    //	printf("\n %d %d",n[0],n[1]);//
     
    printf("%s\n",my_strcat("Hello","world!"));
    }
     
    int my_strlen(char *string1,char *string2){
    int i;
    int *newstring;
    	int *newstring1;
    newstring = malloc(sizeof(string1 +1));
    	newstring1= malloc(sizeof(string2 +1));
     
    	if(newstring == 0|| newstring1==0)
    		printf("no memory");
     
    	printf("%d %d", newstring,newstring1);
    /* for(i=0;*newstring!='\0',*newstring1!='\0';newstring++,newstring1++)
    		i++;*/
    	return (newstring,newstring);
    }
     
    /*char *my_strcat(char*string1, char *string2){
    while(*string1) string1++;
    while(*string1++ = *string2++);
     
    	return(string1,string2);
    }*/
    // warnings and error//
    warning C4013: 'my_strcat' undefined; assuming extern returning int
    : warning C4047: 'return' : 'int ' differs in levels of indirection from 'int *'
    : warning C4101: 'i' : unreferenced local variable
    : error C2040: 'my_strcat' : 'char *(char *,char *)' differs in levels of indirection from 'int ()'
    Error executing cl.exe.


    I think i am making the problem more complicated than it is... i would like to know the concept behind dynamic memory allocation..
    any help would be great!! thanks
    Last edited by Niheel; Oct 6 '06, 02:12 AM.
  • tyreld
    New Member
    • Sep 2006
    • 144

    #2
    You say you figured out how to determine the string length, but the code you posted makes it look like you are trying to do the concatenation in the my_strlen function. Anyways, to concatenate two strings you obviously need to know the lengths of both so you know how much space you need for the new string.

    1.) Your string length function would probably be better just taking one string as input and returning the length of that one string. The basic approach being to loop through the string counting each character up unitl the null byte ('\0'), and returning the final count.

    2.) To concatenate take 2 strings as input. Determine the lenghts of both strings and add these 2 lengths together to get the overall length. Now add 1 to that overall length because you'll need room for the null byte in your new string. Allocate some memory for your new string.

    malloc(sizeof(c har) * (new_string_len gth + 1)) // plus one for the null byte

    Now loop through the first string coping each character into the new string.

    Now loop through the second string and copy each character to the new string (hint: use a standard 0->len of 2nd string for the loop, but when you access the new string use the loop counter + len of the first string to get the right index).

    Finally, add the null byte to the end of the new string.

    Comment

    Working...