Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rajpreet

macrumors newbie
Original poster
Dec 6, 2007
4
0
hello to all here....
i am new to all this programming...i am learning about it ... ..i have theoretical knowledge.. .... can someone tell me about this... i need to know a simple program to concat two strings without using strcat inside operator overloading.plz help me with a program..............
thanks
 

michaelltd

macrumors regular
Mar 30, 2005
142
0
My initial thought would be why not first ensure that both stings are NULL terminating, then create a loop to traverse the first string until it reaches the NULL, then have another loop that starts inserting chars from the second string into the first starting at the position of the NULL, then break as soon as you insert the first NULL you encounter in the second string.

Of course, this naive implementation doesn't take into account whether or not it's writing to a valid location in memory (ie, you might end up writing beyond the amount you allocated for the string and that can be very bad).
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
hello to all here....
i am new to all this programming...i am learning about it ... ..i have theoretical knowledge.. .... can someone tell me about this... i need to know a simple program to concat two strings without using strcat inside operator overloading.plz help me with a program..............
thanks

if you are allowed to use the Standard Template Library's "string" class, then it should be easy. Just do return (string1 + string2);

If you want to do it in a more traditional way, you first need to know the size of the new string. Use strlen to count the characters of both strings, in order to find the size of the new string. Then, use the 'new' operator to allocate memory for your new string array. Then, you will use michaelltd's way of filling the new array. Return the pointer to this array from your function.

PS. Do not forget to delete the allocated memory, otherwise you are creating a memory leak.
 

michaelltd

macrumors regular
Mar 30, 2005
142
0
Yeah, if you can't use the STL string class, then try making one! Should be easy to create your own string class that will dynamically grow the string when needed. All memory allocation can be handled within the class, you can overload operators there for it, and you could even add some statistic stuff for debug purposes (such as keeping track of how many times the string was grown).
 

ChrisA

macrumors G5
Jan 5, 2006
12,914
2,164
Redondo Beach, California
hello to all here....
i am new to all this programming...i am learning about it ... ..i have theoretical knowledge.. .... can someone tell me about this... i need to know a simple program to concat two strings without using strcat inside operator overloading.plz help me with a program..............
thanks

I will not do your home work for you but might try these:

1) Write a for loop for (i=0; stringB; i++) and then inside the lop you move stringB to the end of the first sting
with something like stringA[....]=stingB I'l let you
figure out "...." (it's not hard)

2) If you can';t use strcpy then use something like it. Try memcpy(). But I'd concider this more like finding a loophole in the asignment.
 

rajpreet

macrumors newbie
Original poster
Dec 6, 2007
4
0
hi guys.......thanks for your reply........you guys are very nice,.......
thanks for your time......:D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.