Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   [help] How to Copy String from one array to another without using string.h file (http://www.go4expert.com/forums/help-copy-string-array-using-stringh-t15068/)

anildewani 9Nov2008 14:07

[help] How to Copy String from one array to another without using string.h file
 
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 :disappoin :goofy: :shy:

xpi0t0s 9Nov2008 14:49

Re: [help] How to Copy String from one array to another without using string.h file
 
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 14Nov2008 20:32

Re: [help] How to Copy String from one array to another without using string.h file
 
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 14Nov2008 23:25

Re: [help] How to Copy String from one array to another without using string.h file
 
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 14Nov2008 23:28

Re: [help] How to Copy String from one array to another without using string.h file
 
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 15Nov2008 11:37

Re: [help] How to Copy String from one array to another without using string.h file
 
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 15Nov2008 16:53

Re: [help] How to Copy String from one array to another without using string.h file
 
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 16Nov2008 09:42

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

Originally Posted by back from retirement (Post 38931)
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 16Nov2008 10:35

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

Originally Posted by briff (Post 38998)
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 (Post 38998)
i still have a question on fflush(stdin)

It flushes the input buffer.

briff 16Nov2008 11:26

Re: [help] How to Copy String from one array to another without using string.h file
 
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.


All times are GMT +5.5. The time now is 21:41.