Copying one string from s2[] to s1[]

This is a discussion on Copying one string from s2[] to s1[] within the C++ Programming forums, part of the General Programming Boards category; I have to copy one string from Code: const char s2[] to Code: char s1[] All this is to be ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    13

    Copying one string from s2[] to s1[]

    I have to copy one string from
    Code:
    const char s2[]
    to
    Code:
    char s1[]
    All this is to be done without using the uber elite
    Code:
    <cstring>
    library. So no
    Code:
    strcpy()
    . This is to be done within a function, so assume everything out of the function is properly done. My main question was, without using strcpy, or the cstring library, is there a more efficient way of doing this then I have listed here.

    Code:
    void strcat(char s1[], const char s2[]) {
        int i = 0;
    
        while ( (s1[i] = s2[i]) != '\O')
              i++;
    
        s1[i] = '\O';  // This line is not altogether necessary, but just helps anyway. 
    }
    Thanks for any replies!

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    > s1[i] = '\O'; // This line is not altogether necessary, but just helps anyway.

    How the heck is it supposed to be helpful if not necessary?? And it's '\0' not '\O'

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    13
    Yeah, dunno why I hit an O. I'm still half asleep.

    And it was helpful just because if for some reason the while loop didn't copy over the '\0' then it would insert a new '\0' at the end of the string just to help.

    Array size is irrelevant here because of the fixed length values the function would work with.

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    you won't get out of the while loop unless a '\0' is copied to s1, so I don't see any need to add that line, this will make your function more efficient.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    13
    Ahh, ok. Thanks!

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void strcat(char s1[], const char s2[])

    Just curious, why did you name it strcat if you're doing a string copy?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21