Thread: help with strcpy and arrays with header file

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    26

    help with strcpy and arrays with header file

    I am tyring to do a programm that will use the function strcpy and i have to add the a header file in this code so far this is what i have but it's not doing what it should do
    the output should say this
    Hello
    World............
    Code:
    #include <iostream.h>
    #include <string.h>
    #include "myStrcat.h"
    
    int main ()
    { 
    	char a[20];
    	strcpy(a, "hello");
    		myStrcat(a,"world");
    	return 0;
    
    }
    char* myStrcat(char* dest, const char* src)
    {
    	
        int  i = strlen( src);
         while( *dest ) 
    	src[i++];
    	*dest++; 
    	src[i]; 
    	
    	return dest;
    
    
    }
    C++ Is cool

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Think of it this way:
    dest contains:

    dest[0] = 'h';
    dest[1] = 'e';
    dest[2] = 'l';
    dest[3] = 'l';
    dest[4] = 'o';
    dest[5] = '\0';

    You want to copy into dest, starting at dest[5] (ie dest + strlen(dest) ), the string src, up to and including the terminating zero.

    Does this work? No promises, I didn't compile, but I think it might...

    Code:
    char* mystrcat(char* dest, char* src)
    {
        char* p = dest + strlen(dest);
        char* q = src;
        while ( *p++ = *q++ );
        return dest;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    what should i put then in main function becuze this it compiles but it dosent give u nothing please help
    C++ Is cool

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Code:
    int main ()
    { 
    	char a[20];
    	strcpy(a, "hello");
    	mystrcat(a,"world");
                        cout<<"Output is: "<<a<<endl;
    	return 0;
    
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    IfYouSaySo
    thank you so much it works great see yaa
    C++ Is cool

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing strings in arrays
    By smp in forum C Programming
    Replies: 6
    Last Post: 12-16-2008, 09:37 AM
  2. 2D arrays, strcpy and strcmp Problems
    By Windu102 in forum C Programming
    Replies: 3
    Last Post: 08-23-2008, 01:00 AM
  3. Replies: 2
    Last Post: 05-09-2008, 07:27 AM
  4. Using strcpy() and arrays of structs
    By vital101 in forum C Programming
    Replies: 3
    Last Post: 04-26-2007, 09:04 PM
  5. strcpy error with arrays
    By trang in forum C Programming
    Replies: 4
    Last Post: 01-10-2004, 10:13 PM