Thread: Completely lost...

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

    Completely lost...

    "Write a program that takes a data line at a time and reverses the words of the line"

    For example:

    Input: birds and bees
    Reversed: bees and birds


    I'm not asking for you guys to write it for me obviously, (that kills the spirit of learning)

    So here is what i have so far...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
    char name[80];
    printf("Input: ");
    fgets(name,80,stdin);
    
    
    
    
    printf("Reversed: %s",name);
    
    
    return 0;
    
    
    }
    Uhhh.. am i starting off in the right direction?

    I tried it and it works just fine.
    Last edited by tmac619619; 11-10-2012 at 08:09 PM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    No you do not need the fgets prototype. You can create two pointers to point to the start and end of the array. Reverse the characters in the array(ex. "Hi mom" becomes "mom iH". Once you have the array backwards you can reverse the characters of each word by offsetting your pointers until one reaches a space character(signifying a word). This is just some pseudocode to get you thinking, I am sure there are many ways about doing this, just my input. My best advice is to grab some paper and walk through your algorithms carefully to see what is happening as you reverse characters/words.
    Last edited by camel-man; 11-10-2012 at 08:18 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by camel-man View Post
    No you do not need the fgets prototype. You can create two pointers to point to the start and end of the array. Reverse the characters in the array(ex. "Hi mom" becomes "mom iH". Once you have the array backwards you can reverse the characters of each word by offsetting your pointers until one reaches a space character(signifying a word). This is just some pseudocode to get you thinking, I am sure there are many ways about doing this, just my input. My best advice is to grab some paper and walk through your algorithms carefully to see what is happening as you reverse characters/words.
    Interesting method.

    My instructor used these following functions strtok(),sprintf(),strcpy().

    What should the order be in the use of those functions?
    strcpy first to copy the string. Then use strtok to split up the strings. And finally sprintf to manipulate the string?

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Ok i have made some progress

    Code:
    char name[80];
    char reverse_name[80];
    
    
    printf("Input: ");
    fgets(name,80,stdin);
    
    
    reverse(name,reverse_name);
    
    
    //printf("Reversed: %s",name);
    
    
    return 0;
    }
    
    
    void reverse(char name[],char reverse_name[])
    {
    char *ptr=strtok(name," \n");
    while(strtok(NULL, " \n")!='\0')            //while the user inputted sentence is not '\0'
    {                           
    
    for (i=0;i<80;i++)                                    //continous loop to store the string into the array reverse_name
    {
    strtok(NULL, " \n")=reverse_name[i];
    }
    }
    
    
    
    
    name
    Error:
    reverse.c:36:20: error: lvalue required as left operand of assignment
    Last edited by tmac619619; 11-11-2012 at 02:27 AM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I'm going to assume that as horrible as things look, you're being minimalist.
    Code:
    strtok(NULL, " \n")=reverse_name[i];
    You mixed this up and I think I know what you mean. So here's what to do: Look to the left of the assignment operator, put that on the right; look to the right of the assignment operator, put that on the left. You might also want to help yourself the example code featured here. The way that you are using strtok() is not exactly wrong, but you are taking 0 necessary precautions.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by whiteflags View Post
    I'm going to assume that as horrible as things look, you're being minimalist.
    Code:
    strtok(NULL, " \n")=reverse_name[i];
    You mixed this up and I think I know what you mean. So here's what to do: Look to the left of the assignment operator, put that on the right; look to the right of the assignment operator, put that on the left. You might also want to help yourself the example code featured here. The way that you are using strtok() is not exactly wrong, but you are taking 0 necessary precautions.
    Hey =)

    What do you mean by horrible? Is my program goanna crash?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You posted like half of a main function, that's all. I expected something I could compile from you.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by whiteflags View Post
    You posted like half of a main function, that's all. I expected something I could compile from you.
    Oh my apologies. I don't want to overload this forum with monster length codes, so i tend to post short snippets.

    Code:
    #include <stdio.h>
    #include <string.h>
    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[])
    {
    int i=0;
    
    
    char *ptr=strtok(name," \n");
    
    
    while(strtok(NULL, " \n")!='\0')
    {
    
    
    for(i=0;i<80;i++)
    {
    reverse_name[i]=strtok(NULL, " \n");
    }
    }
    
    printf("Reversed: %c\n",reverse_name[i]);
    
    
    }

  9. #9
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    The fact that you don't bother to check the return value from fgets() hurts my brain.

    Consider this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(void)
    {
        char    buffer[1024];
        char   *line;
        size_t  n;
    
        while (NULL != (line = fgets(buffer, sizeof buffer, stdin))) {
    
            /* Skip leading white-space characters. */
            while (isspace(*line))
                line++;
    
            /* Find the length of the line, excluding newline (CR or LF). */
            n = strcspn(line, "\r\n");
    
            /* Skip trailing white-space characters. */
            while (n > 0 && isspace(line[n - 1]))
                n--;
    
            /* Empty line breaks the loop, ending the program. */
            if (n < 1)
                break;
    
            /* Trim the line, excluding the trailing white-space and newline. */
            line[n] = '\0';
    
            while (1) {
    
                /* Scan backwards, until preceding character is a white-space. */
                while (n > 0 && !isspace(line[n - 1]))
                    n--;
    
                /* Output the string starting there. */
                fputs(line + n, stdout);
    
                /* Are we at the start of the line? */
                if (n < 1) {
                    /* Yes, output a newline, and move to next line. */
                    fputs("\n", stdout);
                    break;
                }
    
                /* We have words before n. Output a space in between. */
                fputc(' ', stdout);
    
                /* Scan backwards over the white-spaces. */
                while (isspace(line[n - 1]))
                    n--;
    
                /* Terminate the line there, then continue backwards scan. */
                line[n] = '\0';
            }
        }
    
        return 0;
    }
    The logic is to first remove leading and trailing whitespace and newline characters from the line.

    Scanning backwards from the end of the line, find the start of the last word. It occurs when you are either at the beginning of the line, or the previous character (to the left) is a white-space. You print that string, skip the previous white-space characters, and terminate the string by replacing the first white-space character after a word with '\0'. Repeat until you have printed the first word in the line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. completely lost
    By jonesk1978 in forum C++ Programming
    Replies: 9
    Last Post: 04-06-2012, 03:01 PM
  2. Completely lost in C#
    By rvbalplaya in forum C# Programming
    Replies: 5
    Last Post: 11-08-2005, 11:49 AM
  3. I'm completely stuck!
    By tek in forum C++ Programming
    Replies: 0
    Last Post: 08-23-2003, 06:43 PM
  4. Completely OOP
    By golfinguy4 in forum C# Programming
    Replies: 4
    Last Post: 04-13-2002, 11:05 PM