hello all

I'm trying to do a concatenation function I do not want to you use the strings property where we can add strings, I need to add two strings and save them in one string without the use of addition "+"

i have three variables title , first_name, last_name I should somehow combine them and save the result inside the full_name string and return it and then print it from the main function. so all the three varaibles should be saved inside one variable which is full name.

what I did here is a void function coz I was not sure how will I do it otherwise ..

there's some other issue which is that the first character of the full_name is ommited and there's no space between first name and last name

here's the code .. your guidence is highly appreciated

# include <iostream>
# include <string>
using namespace std;


void concat( char [], char [], char [], string , int, int, int);
int string_length( char []);

int main()
{
// l for the lengths
int l1, l2, l3;

char title[]="Dr";
char first_name[]="Christina";
char last_name []="Brown";
string full_name;

l1 =string_length(title);
l2 =string_length(first_name);
l3 =string_length(last_name);

concat( first_name, last_name, title,
full_name, l1, l2, l3);

}

int string_length( char anything[])
{
int length=0;

for (int i=0; anything[i]!='\0'; i++)
{
length++;
}

return length;
}

void concat( char first_name[], char last_name[],
char title[], string full_name,
int l1, int l2, int l3)
{

for(int i=0; i<l1; i++)
{
full_name = title[i];
}

for(int i=0; i<l2; i++)
{
full_name = full_name + first_name[i];
}


for(int i=0; i<l3; i++)
{
full_name = full_name + last_name[i];
}

cout << full_name;
cout <<endl;
cout <<endl;

}

You don't need to pass the length of each of those strings. The concat function can easily find the lengths itself.

Here is how to make concat() return the string

string concat(const char* title, const char* fname, const char* lname)
{
    string fullname;
    // concat all three names not shown here

   // now return the result
   return fullname;
}

I'm trying to do a concatenation function I do not want to you use the strings property where we can add strings, I need to add two strings and save them in one string without the use of addition "+"

Instead of trying to decipher your code, I will offer a pseudo-coded solution:

char* concat(string& first, string& second, char* buffer);

string first = "We think in generalities ";
string second = "but we live in details.";

char* concat(string& first, string& second, char* buffer)
{
     int length = 0;

     //Get length of first string
     length = first.size();

     //Add to length of second string
     length += second.size();

     //Allocate enough memory for a cstring buffer
     buffer = new char[length+1];

     //Ensure cstring null termination
     buffer[length] = '\0';

     //Perform concantination
     int i=0;
     while(first[i] != string::npos)
     {
          buffer[i] = first[i];
          i++;
     }
     int j=0;
     while(second[j] != string::npos)
     {
          buffer[i] = second[j];
          i++;
          j++;
     }

     return buffer;
}

You're losing the first character of the full name inside the first for loop (of concat()). The second and third loops add each character to what was in fullname, but the first loop assigns each character to fullname. The second character in title overwrites the first. (If the title were more characters long, you would be missing more.)

As to the spaces that are missing, where did you put them in?

Maybe you should just add a space to fullname between the first and second loops and again between the second and third?

To make it easier on your self. Make a function that concats two strings.

//add two string together and places the result in the result variable
void concat(const char *left, const char* right, char * result);

The you can just concat title with first name. Then the result of that
gets concated with the last name. Then finally display the final result.

The you can just concat title with first name. Then the result of that
gets concated with the last name. Then finally display the final result.

Wrong answer. Those strings don't have enough space allocated to concatenate them (see original post). Each of those strings need to be copied into some buffer that is large enough to hold all the strings at one time.

Wrong answer. Those strings don't have enough space allocated to concatenate them (see original post). Each of those strings need to be copied into some buffer that is large enough to hold all the strings at one time.

Oh, when I said result, I was implicitly talking about a new result string, as suggested by the function prototype that I gave. I thought it was obvious but I guess not.

>>The you can just concat title with first name.

That was my objection. Those two can not be joined.

>>The you can just concat title with first name.

That was my objection. Those two can not be joined.

Are you talking about conceptually or technically?

You don't need to pass the length of each of those strings. The concat function can easily find the lengths itself.

Here is how to make concat() return the string

string concat(const char* title, const char* fname, const char* lname)
{
    string fullname;
    // concat all three names not shown here

   // now return the result
   return fullname;
}

Sir,

are you suggesting this ?

# include <iostream>
# include <string>
using namespace std;

string concat(const char* title, const char* first_name, const char* last_name,int, int, int);
int string_length( char anything[]);


int main()
{
// l for the lengths
int l1, l2, l3;

char title[]="Dr";
char first_name[]="Christina";
char last_name []="Brown";
string full_name;

l1 =string_length(title);
l2 =string_length(first_name);
l3 =string_length(last_name);

cout << concat( first_name, last_name, title, l1, l2, l3);

}

int string_length( char anything[])
{
int length=0;

for (int i=0; anything[i]!='\0'; i++)
{
length++;
}

return length;
}

string concat(const char* title, const char* first_name, const char* last_name, int l1, int l2, int l3)
{   
	string full_name; 

	for(int i=0; i< l1; i++)
		full_name = title[i];
	

    for(int i=0; i<l2; i++)
		full_name = full_name + first_name[i];



	for(int i=0; i<l3; i++)
		full_name = full_name + last_name[i];

  
	return full_name;
}

Instead of trying to decipher your code, I will offer a pseudo-coded solution:

char* concat(string& first, string& second, char* buffer);

string first = "We think in generalities ";
string second = "but we live in details.";

char* concat(string& first, string& second, char* buffer)
{
     int length = 0;

     //Get length of first string
     length = first.size();

     //Add to length of second string
     length += second.size();

     //Allocate enough memory for a cstring buffer
     buffer = new char[length+1];

     //Ensure cstring null termination
     buffer[length] = '\0';

     //Perform concantination
     int i=0;
     while(first[i] != string::npos)
     {
          buffer[i] = first[i];
          i++;
     }
     int j=0;
     while(second[j] != string::npos)
     {
          buffer[i] = second[j];
          i++;
          j++;
     }

     return buffer;
}

hmm .. am not sure i understand why you added the buffer array .. and why you looped buffer "I" and second "J"

would you please clarify ?

You're losing the first character of the full name inside the first for loop (of concat()). The second and third loops add each character to what was in fullname, but the first loop assigns each character to fullname. The second character in title overwrites the first. (If the title were more characters long, you would be missing more.)

As to the spaces that are missing, where did you put them in?

Maybe you should just add a space to fullname between the first and second loops and again between the second and third?

You're losing the first character

yep making me lose my mind!

The second character in title overwrites the first

why does that happen?

Maybe you should just add a space to fullname between the first and second loops and again between the second and third

I tried but nothing happens not a signle space is added !

You don't need to pass the length of each of those strings. The concat function can easily find the lengths itself.

how else would the condition of the for loop work ?

The reason you lose the first character of the title is because the first loop is different from the second and third loop. Look at the difference yourself and see if you can figure it out. You're actually losing all but the last character of the title, but in this case keeping the last means only the first is missing.

If you ever attempted to add spaces, I haven't seen it in your code. Show us where and how you tried to add spaces and it failed.

You don't need to pass the string lengths into concat, if it needs them it can calculate them just like you did. If you use an algorithm that looks for the character that marks the end of the string, it may not need need to know how many characters are in the string anyway.

are you suggesting this ?

string concat(const char* title, const char* first_name, const char* last_name, int l1, int l2, int l3)
{   
	string full_name; 

	for(int i=0; i< l1; i++)
		full_name = title[i];

    for(int i=0; i<l2; i++)
		full_name = full_name + first_name[i];

	for(int i=0; i<l3; i++)
		full_name = full_name + last_name[i];
	return full_name;
}

You can see that that might be close but not right. Why the difference in handling of title vs first_name and last_name in the loops? Wouldn't you suppose they'd all be similar?

nclude <iostream>
#include <string>
using namespace std;

string concat(const char* title, const char* first_name, const char* last_name)
{
   string full_name;

   for ( int i=0; title[i]; i++ )
      full_name = full_name + title[i];

   for ( int i=0; first_name[i]; i++ )
      full_name = full_name + first_name[i];

   for ( int i=0; last_name[i]; i++ )
      full_name = full_name + last_name[i];

   return full_name;
}

int main()
{
   char title[]="Dr";
   char first_name[]="Christina";
   char last_name []="Brown";
   string full_name;

   cout << concat( first_name, last_name, title);
}

/* my output
ChristinaBrownDr
*/

The reason you lose the first character of the title is because the first loop is different from the second and third loop. Look at the difference yourself and see if you can figure it out. You're actually losing all but the last character of the title, but in this case keeping the last means only the first is missing.

If you ever attempted to add spaces, I haven't seen it in your code. Show us where and how you tried to add spaces and it failed.

You don't need to pass the string lengths into concat, if it needs them it can calculate them just like you did. If you use an algorithm that looks for the character that marks the end of the string, it may not need need to know how many characters are in the string anyway.

oh Sir .. I see what you mean

for(int i=0; i<l1; i++)
{
full_name = title; should be full_name += title;
}

regarding spacing I did cout << " "; after the first and the second loops

---new approach--
I tried to take another approach for this problem so I thought of doing the following

# include <iostream>
# include <string>

using namespace std;

string concat (char[], char[]);

int main()
{
	char first_name[]="Sami";
	char last_name[]="Junior";

	cout << (first_name, last_name);
}

string concat (char first_name[], char last_name[])
{
	char full_name[40];
	int j=0;

	for (int i=0; first_name[i]!='\0'; i++)
		{
			full_name[j]=first_name[i];
			j++;
		}

	for (int i=0; first_name[i]!='\0'; i++)
		{
			full_name[j]=first_name[i];
			j++;
		}

	
	return full_name;
	
}

however the whole first name disappears this time


how else would the condition of the for loop work ?

Just check for end-of-string null terminator for(int i = 0; string[i] != '\0'; i++)

Just check for end-of-string null terminator for(int i = 0; string[i] != '\0'; i++)

oh that was an awful mistake .. thanks sir

but it is still not working .. it gives me only the last name there must be something overwriting the other

however the whole first name disappears this time

Call the function...?

cout << (first_name, last_name);

And don't forget to null terminate the full_name in

string concat (char first_name[], char last_name[])
{
   // ...
   full_name[j] = 0;
   return full_name;
}

Call the function...?

And don't forget to null terminate the full_name in

Sir .. I called it right ?
and Sir did you mean by this full_name[j] = 0; we terminate the full_name and not this full_name[j] = '\0'; ?


both cases it still prints only junior

here's the code again I did some modification

# include <iostream>
# include <string>

using namespace std;

string concat (char[], char[]);

int main()
{
	char first_name[]="Sami";
	char last_name[]="Junior";

	cout << (first_name, last_name);
}

string concat (char first_name[], char last_name[])
{
	char full_name[40];
	int j=0;

	for (int i=0; first_name[i]!='\0'; i++)
		{
			full_name[j]=first_name[i];
			j++;
		}

	for (int i=0; last_name[i]!='\0'; i++)
		{
			full_name[j]=last_name[i];
			j++;
		}

	full_name[j] = '\0';
	
	return full_name;
	
}

Sir .. I called it right ?

No.

Spot the difference:

cout << (first_name, last_name);
cout << concat(first_name, last_name);

and Sir did you mean by this full_name[j] = 0; we terminate the full_name and not this full_name[j] = '\0'; ?

Same thing. Pick your favorite.

commented: More than helpful +1

oh it worked >< .. I don't know why things slip off my mind!!

I figured out how to do the space ... as the following :

for (int i=0; i<1; i++)
		{
			full_name[j]=' ';
			j++;
		}

thank you sir

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.