[help] How to Copy String from one array to another without using string.h file

anildewani's Avatar, Join Date: Aug 2008
Newbie Member
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
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
back from retirement's Avatar, Join Date: Nov 2008
Contributor
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();
}
Please report for any inconvenience......cheers.....

----------------------
@ r k @
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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++)) ;
}
back from retirement's Avatar, Join Date: Nov 2008
Contributor
Well....then I will use pointers....e.g.

Code:
void mystrcpy(char *a, char *b)
{
while(*b!='\0')
{
*a=*b;
b++;
a++;
}
*a='\0';
}
I hope this works now.....
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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);
}
briff's Avatar, Join Date: Nov 2008
Newbie Member
Quote:
Originally Posted by back from retirement View Post
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();
}
Please report for any inconvenience......cheers.....

----------------------
@ r k @

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;
    
}
what is the idea of fflush(stdin)? i know it is something related to inputbuffer and inputstream but i cant figure it out... and another thing to note.. how do i store a string of letters into "name"?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by briff View Post
the question is... how do i declare the prototype before my main body?
Same as the function but add a semicolon before it.

Quote:
Originally Posted by briff View Post
i still have a question on fflush(stdin)
It flushes the input buffer.
briff's Avatar, Join Date: Nov 2008
Newbie Member
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];
}
i actually modified to give my
Code:
dest[]={1};
one element of dest's array. the function still works fine. but i just want to find if my understanding is correct... like say if i bring this dest[]={1} into my function and copy with a 4element array.. it means my number of elements in dest will be able to change.