2morow is my exam
and i badly need one program
"how to copy one string from an array to another array without using String.h header file"
waiting for positive responsese
|
Newbie Member
|
|
| 9Nov2008,14:07 | #1 |
|
hey..
2morow is my exam and i badly need one program "how to copy one string from an array to another array without using String.h header file" waiting for positive responsese
|
|
Mentor
|
![]() |
| 9Nov2008,14:49 | #2 |
|
I think what they're asking you to do is to implement your own strcpy function.
If it's a day before your exam how come you don't know how to do that? Must be a very poor course you're on. |
|
Contributor
|
|
| 14Nov2008,20:32 | #3 |
|
It's probably too late....yet I am posting my strcpy....
Code:
#include<stdio.h>
#include<conio.h>
#define m 100
void my_strcpy(char a[m], char b[m])
{
int i;
for(i=0;i<m;i++)
{
b[i]=a[i];
}
return;
}
void main()
{
char a[m], b[m];
printf("Enter your array\t");
scanf("%s", a);
my_strcpy(a,b);
printf("Copied string\t%s", b);
getch();
}
---------------------- @ r k @ |
|
Mentor
|
![]() |
| 14Nov2008,23:25 | #4 |
|
That's more of a memcpy function; it copies m characters regardless of what those characters are. A strcpy doesn't copy a fixed number each time, it just copies until it gets to a terminating NULL.
Plus what if you want to copy an array that isn't exactly m bytes long? If it's less then you'll corrupt memory. If it's more you'll only copy part of the array. |
|
Mentor
|
![]() |
| 14Nov2008,23:28 | #5 |
|
We're way past the deadline (whichever way the date works) so try this one instead (untested):
Code:
void my_strcpy(char *dest,const char *src)
{
while (*dest = *(src++)) ;
}
|
|
Contributor
|
|
| 15Nov2008,11:37 | #6 |
|
Well....then I will use pointers....e.g.
Code:
void mystrcpy(char *a, char *b)
{
while(*b!='\0')
{
*a=*b;
b++;
a++;
}
*a='\0';
}
|
|
Mentor
|
![]() |
| 15Nov2008,16:53 | #7 |
|
Should have tested, dest++...d'oh! And brackets not necessary for *src++. This works, copying the string in just one line; that's why I couldn't believe the OP couldn't figure this out on the day before his exam; it must have been a really crappy programming course if it left people unable to work out a simple strcpy.
Code:
void go4e_38959()
{
char a[32];
char b[32];
char *pa;
char *pb;
strcpy(a,"Hello world");
pa=a;
pb=b;
while (*pb++=*pa++) ; // <- just bung this line in a function to meet the requirement
printf("%s\n",b);
}
|
|
Newbie Member
|
|
| 16Nov2008,09:42 | #8 |
|
Quote:
Originally Posted by back from retirement hi i also need to solve this c++ question in my tutorial. lets say instead i have to declare the my_strcopy before my int main(void) 's body. then i will declare my array as above in the main's body. after which i pass 2 arrays into my_strcopy to copy them.. after my mainbody, i would then define my function. the question is... how do i declare the prototype before my main body? i still have a question on fflush(stdin) lets say i have Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a;
char name;
printf("enter a number\n");
scanf("%d",&a);
fflush(stdin);
printf("enter a name:\n");
scanf("%s",&name);
printf("a is %d, name is %c\n",a,name);
system("pause");
return 0;
}
|
|
Go4Expert Founder
|
![]() |
| 16Nov2008,10:35 | #9 |
|
Newbie Member
|
|
| 16Nov2008,11:26 | #10 |
|
hi another question. i managed to do the coding for my declaration of function and this is what i did:
Code:
void strcopy(char dest[], char src[4]);
int main(void)
{
char src[]="YES", dest[]={1};
int i;
strcopy(dest, src);
printf("%s\n", dest);
system("pause");
return 0;
}
void strcopy(char dest[4], char src[4])
{
int i;
for(i=0; i<4; i++)
dest[i]=src[i];
}
Code:
dest[]={1};
|