#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0

    Simulating strcpy


    I'm trying to simulate the strcpy function using the following code:

    Code:
    #include <stdio.h>
    int main()
    {
    char source[100];
    char destination[100];
    int i=0;
    printf("Enter the source string: "); scanf("%s", &source);
    while(source[i] != '.')
    {
     destination[i]=source[i];
     i++;
    } 
    
    printf("The destination string is: ");
    printf("%s", destination);
      
    getch();
    return 0;    
    }
    The code won't return anything when it meets '.' as I asked it to do. What am I doing wrong?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,171
    Rep Power
    2222
    1. You're not formatting your code.

    Code:
    #include <stdio.h>
    
    int main()
    {
        char source[100];
        char destination[100];
        int i=0;
        printf("Enter the source string: "); 
        scanf("%s", &source);
        while(source[i] != '.')
        {
            destination[i]=source[i];
            i++;
        } 
    
        printf("The destination string is: ");
        printf("%s", destination);
         
        getch();
        return 0;    
    }
    Also, don't put multiple statements on the same line unless you have a very good reason to. You are reducing the readability of your code.

    2. You are ignoring warnings.
    C:TEST>gcc -Wall cannot1.c
    cannot1.c: In function `main':
    cannot1.c:9: warning: char format, different type arg (arg 2)
    cannot1.c:19: warning: implicit declaration of function `getch'

    C:TEST>
    If your program is throwing warnings at you, then it's anyone's guess what the program is actually doing. Until you have cleared up your mistakes that are causing those warnings, it is a complete waste of time to wonder why it's not working.

    Never ignore warnings!

    3. Where does destination get its null-terminator? I do not see you ever terminating destination. That is a very serious error!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,171
    Rep Power
    2222
    PS

    Originally Posted by Cantafford
    The code won't return anything when it meets '.' as I asked it to do.
    What is that supposed to mean?

    The only thing that the code does actually return is a zero to the shell indicating a successful run.

    Are you trying to say that you want the period to be part of the destination string when you're done? If not, then just exactly what are you trying to say?

    Providing sample input and the expected output would help greatly in clarifying your requirements.

    Comments on this post

    • Cantafford agrees
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > The code won't return anything when it meets '.' as I asked it to do. What am I doing wrong?
    Are you typing in say
    This is a sentence.

    Because scanf(%s) will only get as far as "This" before returning.

    If you want to read a WHOLE line, then use fgets().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0
    Originally Posted by dwise1_aol
    PS


    What is that supposed to mean?

    The only thing that the code does actually return is a zero to the shell indicating a successful run.

    Are you trying to say that you want the period to be part of the destination string when you're done? If not, then just exactly what are you trying to say?

    Providing sample input and the expected output would help greatly in clarifying your requirements.
    I'll give you an example. I have a string A(source) entered by the user ended with the '.' character. When the compiler meets the '.' character it should copy string A in sting B.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0
    Originally Posted by dwise1_aol

    3. Where does destination get its null-terminator? I do not see you ever terminating destination. That is a very serious error!
    I did it like you said:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char source[100];
        char destination[100];
        int i=0;
        printf("Enter the source string: "); 
        gets(source);
        while(source[i] != '\0')
        {
            destination[i]=source[i];
            i++;
        } 
        destination[i]='\0';
    
        printf("The destination string is: ");
        printf("%s", destination);
         
        getch();
        return 0;    
    }
    Worked. Thanks!

    Comments on this post

    • dwise1_aol disagrees : We still don't know what you were trying to do!
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,171
    Rep Power
    2222
    Originally Posted by Cantafford
    I'll give you an example. I have a string A(source) entered by the user ended with the '.' character. When the compiler meets the '.' character it should copy string A in sting B.
    First you say that you'll give us an example. Then you don't give us one.

    GIVE US THE EXACT STRING THAT WAS INPUT!

    What part of that is so difficult to understand?

    Refer to salem's question to see why it is so important for you to give us the exact input string, not an inexact description of it.

    If you refuse to help us to help you, then we cannot help you.

IMN logo majestic logo threadwatch logo seochat tools logo