View Single Post
Old 04-14-2004, 11:09 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
As I stated before we have to concatenate the 2 strings (3 with 2 appended) and it has to be generic so that it would work for any text that could fit in those strings, so that it reads "My name is Fred" as if it were all in one array.
What a jolly coincedence! I just finished 25% of a tutorial about operator overloading. I went over here to test the layout so far, and noticed your post. Let me give you an excerpt of this tutorial. Then find out if you can solve it on your own. If you can't then come back again, then I'll gladly show you how in detail.

Tutorial starts here.
About basic charater and pointer handling.
Do you remember that:
char* szMyCharVariable="mytext";is exactly the same as char szMyCharVariable[]="mytext";? You will do this when the size of the assigned string is unknown beforehand. Observe the code below:
Code:
#include <iostream>
 
using namespace std;
int main()
{
	char* szHello = "Hello";
	char szHowdy[] = "Everybody!";
	
	return 0;
}
Using the code above, we would like to show "Hello everybody!" to our console output, but first we would like to merge these two strings into a third string called "szGreetAll".
This is what I encounter often when students ask for help:
Code:
#include <iostream>
 
using namespace std;
int main()
{
	char* szHello = "Hello";
	char szHowdy[] = "Everybody";
	char* szGreetAll = szHello+szHowdy; //It doesn't work this way.
	cout<<szGreetAll<<endl;
	
	return 0;
}
The code above doesn't show proper character handling.
First of all szHello+szHowdy;. You are merely trying to add two pointers. That is illegal to start with.
And secondly one shouldn't use the assignment operator on char(s) at all.
Some try to merge strings by using a for-loop and iterating through all the elements. But that is not efficient. There are special methods for assigning characters to characters:
strcpy is the most important one. And adding charracters to form a bigger string offers another method: strcat. I'm sure you've read here and there that characters are represented as arrays. So to add szHello and szHowdy, you need a third character array that can hold both characters. Lets find out how we can calculate the size of szGreetAll. Yes we can count the number of characters in this case, but often we don't have that luxury. So lets review the way to actually calculate the needed size by ourselves:
Code:
#include <iostream>
 
using namespace std;
int main()
{
	char* szHello = "Hello";
	char szHowdy[]="Everybody";
	
	//strlen() returns the size in bytes, so we need to add +1 later. Guess why.	
	int len = strlen(szHello);
	int len2 = strlen(szHowdy);
	len+=len2; 
	//So szGreetAll has the size of szHello and szHowdy PLUS 2 for the terminating character and...
	//...for the extra space we insert between szHello and szHowdy!
	char* szGreetAll=new char[len+2];
	//Line below: the way to copy and merge strings.
	strcpy(szGreetAll, szHello); //copy szHello to our buffer.
	strcat(szGreetAll, " "); //Merge it with an extra space.
	strcat(szGreetAll, szHowdy); //And then glue szHello to the end of the buffer.
	cout<<szGreetAll<<endl;
	
	return 0;
}
Enough about character handling. We will use these techniques in our custom defined String class later.
Tutorial ends here.
Do you see the point I am making? Try to use:
  • strlen()
  • strcpy()
  • strcat()
These form the very basics of proper string handling.
__________________
Valmont is offline   Reply With Quote