Does anyone have an example of copying a string without using strcpy?
Printable View
Does anyone have an example of copying a string without using strcpy?
Just today Stack Overflow posted such, in Example 2.4.
Code:memcpy(dest, src, strlen(src) + 1);
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.
Hello,
» Heh. This question gets asked alot. Speaking of effort, how's this:Quote:
Originally Posted by quzah
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. :DCode: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;
}
- Stack Overflow
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.
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;
}
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. :rolleyes:
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.
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
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.And I am not gee. As you can see.Code:printf("Str.... %s", str1);
Thanks AndyHunter I usualy delite system commands, but I forgot now.
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:
Your quoted code is also wrong, because you have:Code:printf( "foo is %s", &foo );
Which is wrong because:Code:printf("Str.... &d", str1);
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.
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`;
}