1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'C' started by anildewani, Nov 9, 2008.

  1. anildewani

    anildewani New Member

    Joined:
    Aug 22, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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:
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    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 @
     
    shabbir likes this.
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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++)) ;
    }
    
     
  6. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    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.....
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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);
    }
    
     
  8. briff

    briff New Member

    Joined:
    Nov 16, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0

    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"?
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Same as the function but add a semicolon before it.

    It flushes the input buffer.
     
  10. briff

    briff New Member

    Joined:
    Nov 16, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Um, no. It may appear to work fine in this simple example, but in fact the code suffers from a buffer overflow.

    Since you don't specify the size of dest, the compiler deduces it from what's between the braces. {1} is just one item long, so dest is actually of type char[1], which is probably the single most pointless declaration in C, because you have to use pointers and by using array terminology people would be expecting a string here, not a single character, and you only have space for a terminating NULL. If you want a single char, declare dest as "char dest;" and drop the array syntax.

    So dest[1..3], which are written to in strcopy REGARDLESS of the actual length of the string in src[], are not part of the dest array; they belong to someone else, and you've just overwritten their memory for them.

    Why are you only copying 4 bytes regardless of the size of the string in src? As I said to another poster, this is more of a memcpy function than a strcpy. strcpy should take string semantics into consideration, i.e. it should be written according to the fact that a string is a number of characters terminated with a zero byte. So the end of the copy loop should end after copying the zero byte, not after a fixed number (unless you're implementing strncpy).
     
  12. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    Yes.....it will change I think, because you have not specified the size of dest initially....else you would have to allocate it dynamically....
    Still I wait for the expert's opinion to be absolutely confirm....
     
  13. manju154

    manju154 New Member

    Joined:
    Oct 18, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    
    void stringcopy(char *, char *);
    int main()
    {
        int i;
        char src[] = "hai this patil";
        char *dest = (char*)malloc(strlen(src)+1);
    
        stringcopy(dest , src);
        printf("source  = %s\n",src);
        printf("destination = %s\n",dest);
    
    }
    void stringcopy(char *t , char *s)
    {
        while ((*t = *s) !=0) {
            s++;
            t++;
        }
    }
     
    Last edited by a moderator: Oct 18, 2011

Share This Page