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

  1. #16
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by camel-man View Post
    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.
    According the article, Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

    Code:
    while(fgets(name,80,stdin)!=NULL)


    So, fgets will never equal to null because the value of fgets is a pointer. So is it possible to have a terminating condition in this case?


  2. #17
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Why do you have the while loop? Why cant you just use the strcpy?

  3. #18
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by camel-man View Post
    Why do you have the while loop? Why cant you just use the strcpy?
    Yeah i realized that the while loop involving fgets is completely out there, and isn't correct.

    Solution calls for:

    char temp[80], reverse[80], *ptr;

    Use strtok to get the first part of the string and save it to the ptr pointer. Then using sprintf, copy it to the reverse array.
    Use strpy to copy reverse into another array, temp. Repeat. Now the key to printing it backwards is to keep using sprintf, with ptr in front of
    temp.

    reverse = sprintf("%s %s", ptr, temp);

    That's why im using a while loop.
    Hopefully i implement this correctly

    Code:
    
    
    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[])
    {
    
    char *ptr=strtok(name, " \n");                                    //Use strtok to get the first part of the string and save it to the ptr pointer. Then using sprintf, copy it to the reverse array. 
    reverse_name=sprintf("%s,ptr);
    
    strcpy(temp,reverse);                                                                                                             // Use strcpy to copy reverse into another array, temp. Repeat. Now the key to printing it backwards is to keep using sprintf, with ptr in front of
                                                                                                                                                            temp.
                                                              
    
    
    
    
    
    
    
    
    
     printf("%s\n",temp);
    }
    
    
    I wish i can see this in action.
    Last edited by tmac619619; 11-11-2012 at 08:46 PM.

  4. #19
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    I'm making lots of progress thanks guys, but here's more tidbits of code

    Code:
    void reverse(char name[],char reverse_name[],char temp[])
    {
    
    strcpy(temp,name);           //copy whatever user inputs into temp array
    char *ptr=strtok(temp, " \n");      // split input. e.g. bees and birds into bees\0and\0birds\0
    
    
      while(strtok(NULL, " \n")!='\0')    // if input is "birds and bees" it starts off at "and",and should print it to reverse_name array. Am i using sprintf correctly in this case? 
                                                  //removing the while loop and its contents fixes Seg fault error. Weird
    {
     sprintf("%s",reverse_name);
    
    }
    
    
     printf("%s\n",reverse_name);
    
    
    }
    Basically i want to store the string from the while loop into an array
    Last edited by tmac619619; 11-11-2012 at 10:20 PM.

  5. #20
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You should really start to read (and understand) the manual pages for the functions you want to use.

    Code:
    while(strtok(NULL, " \n")!='\0')
    You have started the tokenization correctly by storing the pointer to the first token in "ptr". But your while condition just throws away every following token because you don't store the return value of strtok().

    Code:
    sprintf("%s",reverse_name);
    The first argument of sprintf() should be a pointer to the string you want to print to.

    Bye, Andreas

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