Thread: strcpy not working as intended? (5 lines of code)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    strcpy not working as intended? (5 lines of code)

    Code:
    char name[80];
    char reverse_name[80];
    
    
    
    printf("Input: ");      //prompt user for input of a string
    fgets(name,80,stdin); //store string
    
    
    
    strcpy(reverse_name,name);
    
    
    printf("Print string: %s\n",reverse_name);
    Let's say input was birds and bees.
    my printf only displays birds.
    Why is that?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Can't reproduce:
    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <string.h>
     
    int main(void)
    {
        char name[80];
        char reverse_name[80];
     
        printf("Input: ");      //prompt user for input of a string
        fgets(name,80,stdin); //store string
        strcpy(reverse_name,name);
        printf("Print string: %s\n",reverse_name);
        return 0;
    }
    $ make foo
    cc -ggdb3 -Wall -Wextra    foo.c   -o foo
    $ ./foo
    Input: birds and bees
    Print string: birds and bees
    Can you post more of the program you are running?

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by AndiPersti View Post
    Can't reproduce:
    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <string.h>
     
    int main(void)
    {
        char name[80];
        char reverse_name[80];
     
        printf("Input: ");      //prompt user for input of a string
        fgets(name,80,stdin); //store string
        strcpy(reverse_name,name);
        printf("Print string: %s\n",reverse_name);
        return 0;
    }
    $ make foo
    cc -ggdb3 -Wall -Wextra    foo.c   -o foo
    $ ./foo
    Input: birds and bees
    Print string: birds and bees
    Can you post more of the program you are running?

    Bye, Andreas
    Yes
    Code:
    void reverse(char name[],char reverse_name[]);
    
    
    int main()
    {
    char name[80];
    char reverse_name[80];
    
    
    printf("Input: ");
    fgets(name,80,stdin);
    
    
    reverse(name,reverse_name);
    
    
    return 0;
    }
    
    
    void reverse(char name[],char reverse_name[])
    {
    
    
    char *ptr=strtok(name," \n");
    
    
    //while(strtok(NULL, " \n")!='\0')
    //{
    
    
    strcpy(reverse_name,name);
    //printf("Reversed: %s\n",reverse_name);
    //}
    printf("Print string: %s\n",reverse_name);
    Basically if i do the strcpy in main, it prints out the entire string correctly. But if i do strcpy within the function reverse, it only prints out the first word.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Using strtok will write in '\0' characters inside your string after the delimiter, which effectively makes it a smaller string with just the first word. Thats why strcpy only copies the first word. I would recommend making a temporary copy of your string, and then using strtok on the temporary copy.

    Code:
    char tmp[strlen(name)];
    strcpy(tmp, name);
    
    char *token = strtok(tmp, " \n");
    // ... (do processing here and save your result into reverse_name)

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    compiler doesn't accepts anything after whitespace so make sure your input doesn't have any whitespace.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Ok more problems

    Code:
    printf("Input: ");
    fgets(name,80,stdin);
    
    
    reverse(name,reverse_name,temp);
    
    
    return 0;
    }
    
    
    void reverse(char name[],char reverse_name[],char temp[])
    {
    
    
    
    
       while(fgets(name,80,stdin)!='\0')
    {
       strcpy(temp,name);
    }
    
    
    printf("%c\n",temp[0]);
    }
    When i print out temp[0],it should print out whatever string the user inputted correct?
    e.g.
    temp[0]="Whatever the user typed in"

    But when i run it prints nothing, and after i type enter, it keeps printing out blank lines

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    printf("%c\n",temp[0]);
    how is that supposed to print out the string? %c is a character %s is a string.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    I also tried

    printf("%s\n",temp); prints out blank lines

    printf("%s\n",temp[0]); :error

    I'm lost here, please help me camel

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    while(fgets(name,80,stdin)!='\0')
    You might be over thinking this, shouldn't you have
    Remember that NULL and '\0' are not the same thing ex)
    Code:
    while(fgets(name,80,stdin)!=NULL)
    makes more sense I would say.
    Although I am confused on to what you want your while loop to achieve??
    Last edited by camel-man; 11-11-2012 at 07:55 PM.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Just checked my textbook

    fgets returns a '\n' before '\0', if there is space.

    Code:
      while(fgets(name,80,stdin)!=NULL)
    {
       strcpy(temp,name);
    }
    
    
     // strcpy(reverse_name,name);
    
    printf("%s\n",temp);
    }
    This should still work though. The logic makes sense, doesn't it?
    But blank lines are still being printed.

    looks like... strcpy didn't work.
    Last edited by tmac619619; 11-11-2012 at 07:53 PM.

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by tmac619619 View Post
    Just checked my textbook
    If you want to get a reference here you are fgets

    Read the description of the function.It is very good.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by camel-man View Post
    Post your revised code so people here trying to help can compile.
    Code:
    void reverse(char name[],char reverse_name[],char temp[]);
    
    
    int main()
    {
    char name[80];
    char reverse_name[80];
    char temp[80];
    printf("Input: ");
    fgets(name,80,stdin);
    
    
    reverse(name,reverse_name,temp);
    
    
    return 0;
    }
    
    
    void reverse(char name[],char reverse_name[],char temp[])
    {
    
    
    
       while(fgets(name,80,stdin)!=NULL)
    {
       strcpy(temp,name);
    }
    
    
    
    
     printf("%s\n",temp);
    }
    output:
    Input: hi my name is
    (blank line)
    (blank line)
    (blank line)
    (more blank lines)
    Last edited by tmac619619; 11-11-2012 at 07:57 PM.

  13. #13
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Take a close look at your while loop condition and tell me what you think it does. Look over the link that std10093 has posted for you, it will help you understand fgets.
    Last edited by camel-man; 11-11-2012 at 08:05 PM.

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by camel-man View Post
    Take a close look at your while loop condition and tell me what you think it does.
    Ok.

    When user inputs "hi my name is joe"
    there would be a '\n' after user presses enter key (fgets function)
    So in the while loop once enter key is pressed and a '\n' is added on to the last word.

    printf("%s\n",temp)

    ^ obviously i know that the logic above is wrong somewhere, but to me it makes sense

  15. #15
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Now tell me what makes you think that the while loop will terminate once it reaches a newline? Where in your while loop does it "state" that? Remember that fgets returns a char*, like I said before I think you should check out std10093's link that he provided for you, it will help you understand fgets better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-19-2010, 07:42 AM
  2. Not working as intended
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2008, 11:10 AM
  3. Lines of code per day.
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 10-26-2007, 09:20 AM
  4. "User Input" not working as intended
    By daedenilus in forum C Programming
    Replies: 3
    Last Post: 11-13-2005, 05:34 PM
  5. constructor not working with strcpy()
    By whackaxe in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2004, 12:15 PM