Thread: Combining two char arrays into a third one

  1. #1
    Unregistered
    Guest

    Combining two char arrays into a third one

    Ok i have tried this and I know I am doing something wrong but I cant fig out what for the life of me..this is prob totally wrong

    #include <iostream>
    using namespace std;

    int main()
    {
    char A[4]="abc";
    char B[4]="def";
    char C[7];


    for( int i=0;i<=4;i++ )
    C[i] = A[i];


    for( i=0;i<=4;i++ )
    C[i+4] = B[i];



    cout <<C <<endl;


    return 0;

    }

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Using from 0 to < n is typical in C and C++, rather than 0 to <= n -1, when you want to do something n times.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char A[4]="abc";
    	char B[4]="def";
    	char C[7];
    	int i;
    
    	for(i=0;i<3;i++ )
    	C[i] = A[i];
    
    	for( i=0;i<3;i++ )
    	C[i+3] = B[i];
    
    	C[6]='\0'; // terminate the char*
    
    	cout <<C <<endl;
    
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Unregistered
    Guest
    ohh thank you for that help...I would hug you if I knew you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM