help - string copy from character position 0 to a designated character position

This is a discussion on help - string copy from character position 0 to a designated character position within the C Programming forums, part of the General Programming Boards category; these function are called from main - I am just trying to copy string st1 to string st2 from character ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    Unhappy help - string copy from character position 0 to a designated character position

    these function are called from main - I am just trying to copy string st1 to string st2 from character position 0 to character position unsigned int to (3 as in this example) but I can't figure out where to stop the increment in order to stop at the unsigned int to.....

    any help would be appreciated....

    Code:
    void g_strtcpy()
    {
    	char ST1[30]="abcdefg";
    	char ST2[30]="";
    	
    
    
    	/*Example 1*/
    	printf("\n\n strtcpy() function demonstration #1:");
    	printf("\n Input string is =%s= ",ST1);
    	printf("\n Output string is =%s= ",ST2);
    	printf("\n Copy to character position 3");
    
    	strtcpy(ST2,ST1,3);
    
    	printf("\n\n Input string is =%s= ",ST1);
    	printf("\n Output string is =%s= ",ST2);
    	printf("\n\n");
    
    }
    void strtcpy(char *outstring, char *instring, unsigned int to)
    {
    	int i=0;
    	int j=0;
    	int k=to-1;
    	
    	if (k<=-1)
    		{
    			outstring[0]=EOS;
    			
    		}
    
    	else if (k>=0)
    		{	
    		
    			while (instring[j]!=EOS)
    			{	
    				outstring[i]=instring[j];				++i;
    				++j;
    				
    
    			}
    			
    			outstring[i]=EOS;
    		}
    	
    	
    }

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    void CopyString(char* Destination, char* Source, int Length)
    {
       for(int i=0; i<Length; i++)
       {
          Destination[i] = Source[i];
       }
       Destination[Length] = '\0';
    }
    Of course, this assumes that the destination buffer is large enough and that length isn't larger than the size of the source string.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    thanks for the help

    Thank you both for replying...especially so quickly...

    your info was greatly appreciated and I will try it just as soon as I can.

    Thanks again

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a novel idea: strncpy

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. strcpy - How to copy a single character to string?
    By rockysfr in forum C Programming
    Replies: 3
    Last Post: 09-24-2005, 03:12 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21