Thread: my strcat function

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    115

    my strcat function

    Ive tried making my own strcat function but i need help in returning the array or whole string in the my_strcat function.
    How do i return that array?
    Im trying strcat with array notation, but kidna stuck.
    cheers

    Code:
    #include <stdio.h>
    #include <strings.h>
    
    #define SIZE 1000
    
    void get_words( char [] );
    char *my_strcat( char [], char [] );
    
    int main()
    {
    	char words[SIZE];
    	char words2[SIZE];
    
    	printf( "Enter String1:\n" );
    	get_words( words );
    	printf( "Enter String2:\n" );
    	get_words( words2 );	
    
    	/*	printf( "Enter String 2:\n" );
    		get_words2( words2 );
    	*/
    
    	printf( "string1: %s", words );
    	printf( "\n" );
    	printf( "string2: %s", words2 );
    	printf( "\n" );
    
    	printf( "concat: %s", my_strcat( words, words2 ) ); 
    
    	return 0;
    }
    
    void get_words( char string[] )
    {
    	int ch;
    	int n=0;
    	
    	while( (ch=getchar()) != '\n' ) {
    		string[n] = ch;
    		n++;
    	}
    }
    
    char *my_strcat( char words1[], char words2[] )
    {
    	int n=0;
    	int i=0;
    
    	char temp[SIZE];
    
    	/* copy words1 into temp without the null byte */
    
    	while( words1[i] != '\0' ) {
    		temp[n] = words1[i];
    		n++;
    		i++;
    	}
    	i=0;	/* reset i to zero */
    	/* insert a blank space after the word being copied */
    
    	temp[n] = ' ';
    
    	/* copy words2 in temp after the blank space */
    	
    	while ( words2[i] != '\0' ) {
    		temp[n] = words2[i];
    		i++;
    	}
    
    	/* insert a null byte at end of temp for complete string */
    
    	temp[n] = '\0';
    
    	return temp;
    }
    there are only 10 people in the world, those who know binary and those who dont

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Because 'temp' is defined in the my_strcat() routine, once you return, the array is gone. You need to declare temp as:
    static temp[x];

    to keep the array alive.

    Also copying your second word you never incremented 'n'. That might help a little.

    Walt

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    thanks guys.

    just another problem.
    in some books i see a declaration such as:

    my_array[n] = '\0';

    why does it attach the null byte to the end of the string when the n has not been incremented? ive tried this in my program for the blank space but it wont work, i need to increment it by one.
    so i did

    temp[n+1] = ' ';

    how can the null byte just be inserted in the array without any sort of incrementation.?
    there are only 10 people in the world, those who know binary and those who dont

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM