about c++

Discussion in 'Mac Programming' started by rajpreet, Dec 6, 2007.

  1. rajpreet macrumors newbie

    Joined:
    Dec 6, 2007
    #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
     
  2. michaelltd macrumors regular

    michaelltd

    Joined:
    Mar 30, 2005
    #2
    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).
     
  3. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    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.
     
  4. michaelltd macrumors regular

    michaelltd

    Joined:
    Mar 30, 2005
    #4
    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).
     
  5. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #5
    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.
     
  6. rajpreet thread starter macrumors newbie

    Joined:
    Dec 6, 2007
    #6
    hi guys.......thanks for your reply........you guys are very nice,.......
    thanks for your time......:D
     

Share This Page