Thread: Copy String without using strcpy

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    Copy String without using strcpy

    Does anyone have an example of copying a string without using strcpy?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Just today Stack Overflow posted such, in Example 2.4.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    memcpy(dest, src, strlen(src) + 1);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Doesn't anyone around here put forth any effort on their own? How the ........ do you think you'd do it? Seriously. Did you give this any thought at all?

    "Hmm.. how would I copy a string myself?"
    "Well, I'd start by writing the first letter. Then I'd move on to the next."
    "I'd probably repeat that until I was done!"
    "OMG! Could I do that in code!?"
    "I bet I could do that while I subconciously think of something else to not put any effort into!"
    "I mean, seriously, why bother, when everyone will do it for me?"

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Hello,
    Quote Originally Posted by quzah
    Doesn't anyone around here put forth any effort on their own?
    » Heh. This question gets asked alot. Speaking of effort, how's this:
    Code:
    unsigned int _O0(const char *_O0) {
        const char *_OO;
    
        _OO = _O0;
        while (*_OO)
            _OO++;
        return _OO - _O0;
    }
    
    void *_0O(void *_O0, const void *_OO, unsigned int _00) {
        while (_00--)
            ((char *)_O0)[_00] = ((char *)_OO)[_00];
        return _O0;
    }
    
    int main() {
        char _00[5], _OO[5] = "_O0";
        _0O(_00, _OO, _O0(_OO) + 1);
        return 0;
    }
    Just for the fun of it. Never done it before, and my first time too. At least it compiles with -Wall, -pedantic, -ansi, and -std=c89 GCC flags.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're running across the reserved namespace here. Add a O to the beginning of all those underscores. Then you'll get something like this!

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i = 0;
        char str1[30], str2[30]; 
        printf("Insert str1\n");
        scanf("%s", &str1);
        while(str1[i] != '\0')
        {
                      str2[i] = str1[i];
                      i++;
        }
        printf("Str1 looks like ... %s\n", str1);
        printf("Str2 looks like ... %s\n", str2);
        return 0;
    }
    Last edited by xxxrugby; 02-05-2005 at 06:11 PM.
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, gee, it's sure a good thing you did that, because none of us knew how. I mean, the only reason we didn't just post full working code is because we couldn't figure it out.

    Oh, and your code is wrong. The second string won't have a null terminator, and when you try and use it, you'll merrily run off the end of your array.

    Also, your printf statement is wrong.

    So if you are actually going to do everything for someone, make sure you're actually doing it correctly.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Ahh, and the system call at the end just wraps it up. Lets try to stay away from system specific implementations for example programs unless you specify where the system dependency lies.

    Take a look at the FAQ
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  10. #10
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    quazah I am a newbie. And I sow your code up, and I dont understand it. So I think what question is. That maybe the man is also like I and will not understand a bit.
    I see where my printf statment is wrong. Thanks.
    Code:
    printf("Str.... %s", str1);
    And I am not gee. As you can see.
    Thanks AndyHunter I usualy delite system commands, but I forgot now.
    Last edited by xxxrugby; 02-05-2005 at 07:15 PM.
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you've edited your post as to make your printf statements correct now. However, you had them wrong. You don't use the & operator when using printf, unless you're trying to print the address of a varaible.

    You had:
    Code:
    printf( "foo is %s", &foo );
    Your quoted code is also wrong, because you have:
    Code:
    printf("Str.... &d", str1);
    Which is wrong because:
    1) You would be using %, and not the & there.
    2) If that's supposed to be a specifier, you've got d, and if that's a string, it should be s.

    Which would end up:
    Code:
    printf("Str.... %s", str1);

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    As a function:
    Code:
    void mystrcpy(char *d, const char *s) {
         int x;
         for(x = 0; s[x] != '\0'; x++)
              d[x] = s[x];
         d[x] = '\0`;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. strcpy - How to copy a single character to string?
    By rockysfr in forum C Programming
    Replies: 3
    Last Post: 09-24-2005, 03:12 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM