Thread: Appending Strings

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    Appending Char

    Well I am having problems prety bad, want to make a function in which can concatenate one string to another. I have to do it with a pointer notation and a array notation


    Code:
    char* strcat(char *str, const char *str2)
    {
    
    }
    Code:
    char* strcat(char str1[], const char str2[])
    {
    
    }
    Can anyone help me?
    Last edited by Fuyuki; 11-17-2005 at 10:12 AM.

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    When you say strings do you mean the actual string class or an array of char?
    If so, then why not use the append?
    http://www.cppreference.com/cppstring/append.html

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    I meant to say char, and I am not allowed to use append I have already something in the lines of


    Code:
    char *result = str1;
    
    while(*str1 != "/0")
    {
    
    str1++;
              while( *str2 != "/0")
              {
                          *str1 = str2;
                          str1++;
                          str2++;
               }
    }
    return *result;
    I dont think that wokrs though anyone can confirm that one for pointers? arrays I have no idea how I am going to do it

    also its char, not strings sorry bout that

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    *double post*

    Anyone anyone??

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your code is close. What you are trying to do is loop to the end of str1, then loop through str2 adding each of its characters to str1, right? Your first loop should end before the second loop starts so that the str2 characters will be tacked on at the end, but it doesn't in that code.

    You also have a typo where you forgot to dereference the str2 pointer, and you need to make sure you add a '\0' to the end of str1 after you're done. Finally, I believe the methods should be identical for arrays or pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. Appending strings
    By Ajsan in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 12:56 PM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM