Gooday
i have a question to make a program using the connect function and all i hve done is below. how do i complete the the first loop and the second loop...on what i should do next here is my code so far, explaination will be appreciated....

#include <iostream>
 using namespace std;

 void connect( char *s1, const char *s2 ) {
  while ( *s1 != '\0' ) {
  //how do i complete this loop
}
while ( *s2 != '\0' ) {
// how do i complete this loop? and how do i  assign each character of s2 to s1.
 }
 }
 int main()
 {
 char string1[ 20 ];
 char string2[ 20 ];

 cout << "Enter two strings: ";
 cin >> string1 >> string2;
 connect( string1, string2 );
 cout << string1 << endl;
 return 0; // indicates successful termination
 } // end main

my goal is to cme up withan outcome of

Enter two strings: abc cba
abccba

1. The function you are trying to implement is usually referred to as "concatenation" (or just "cat" for short). I'm just pointing that out to help you build a technical vocabulary for computer science.

2. There are already functions that implement this functionality. I'm sure you knew that, but just in case you didn't know. For C-style string (i.e. char*), the function is called strcat() and it does exactly what you want to do. Now, if you use C++ strings (i.e. std::string), then it is even simpler, either use the function append() or simply use the addition operator like you would with any other type.

3. As I assume you knew the above, and thus, you are doing this as a learning exercise only. It is hard to know how much help I should provide, because coming up with the logic for your algorithm is usually about 95% of the work (1% is the time to implement it and 4% is the time to fix the problems with it.. your mileage may differ). So basically, I would say the first logic step is to simply get to the end of the first string, then copy character after character from the second string to the first until you reach the end of the second string, put the null character at the end and you're done. I really cannot explain it in any simpler terms.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.