Thread: Displaying an input backwards

  1. #16
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    The book didn't say anything about ^ or getline yet, so those weren't really an option I guess (yet). I was thinking of reading through the entire book, then going through the entire book again (and again, etc. as necessary). Or do you think it's better to carefully study one chapter at a time as much as possible?

    Maybe I'm tired...I can't find a way to do the actual question...is it saying to do scanf("%c", line) then declare char line[255] or something like that? I don't know how this can read a character at a time like we just did..
    Last edited by guitarscn; 03-12-2008 at 12:11 AM.

  2. #17
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Yes, I would definitely go slowly and learn everything very well before moving on. You will have a hell of a time with the more advanced topics unless you have a very solid understanding of the basics.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #18
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    This is the best I could come up with:



    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
            int line;
            char count[255]; /* set a max array of 255 characters */
    
            printf("enter a line: ");
            for (line = 0; ; ++line)          /* start at array block 1, let the user input a character, */
                    scanf("&#37;c", count[line]); /* continue input with next block (++line) until press Enter */
    
            for (line = 255; line >= 0; --line) /* start at the last character block and go down to the first */
                    printf("%c", count[line]); /* print all characters on the whole array going backwards (--line) */
    
            printf("\n");
    
            return 0;
    }


    I can compile it fine, but when I try the program, after I input a line and press Enter, it gives me a "Segmentation fault (core dumped)" error.
    Last edited by guitarscn; 03-12-2008 at 01:08 AM.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
            for (line = 0; printf("\n") != 1; ++line) /* start at array block 1, as long as you don't press enter, */
                    scanf("&#37;c", count[line]);         /* let the user input a character on the next block (++line) */
    if you want safly read line use

    Code:
    if(fgets(count, sizeof count, stdin) == NULL)
    {
       /* failed to read line - notify and exit */
    }
    else
    {
       /* line read */
       char* p = strchr(line, '\n');
       if(p)
       {
          /* new line char found - remove it */
          *p = '\0';
       }
       /* here process the line as you need */
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    Ah, these chapters have not yet covered 'if' and 'else' stuff yet. Also definitely not covered strchr and not even stdin either, I think. I know what NULL is so that's fine even though the book did not teach to use it yet. Also, I am not familiar with the char* and *p stuff either.

    The question is asking for the answer only using the very basics that have been convered in the chapters so far...no doubt anyone here can probably come up with a very sweet and short solution, but I guess it's pretty weird to just use the basic stuff.
    Last edited by guitarscn; 03-12-2008 at 01:14 AM.

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by guitarscn View Post
    Ah, these chapters have not yet covered 'if' and 'else' stuff yet. Also definitely not covered strchr and not even stdin either, I think. I know what NULL is so that's fine even though the book did not teach to use it yet. Also, I am not familiar with the char* and *p stuff either.

    The question is asking for the answer only using the very basics that have been convered in the chapters so far...no doubt anyone here can probably come up with a very sweet and short solution, but I guess it's pretty weird to just use the basic stuff.
    In that case - I will stick with the original version with scanf and leave the advanced stuff till you learn the corresponding functions
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #22
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    I am pretty satisfied with the way the original solution came out - but NeonBlack did make a suggestion that I try to follow the instructions in the beginning, which I did not follow to get my current solution. So basically I'm trying to solve this all over again, but following the directions which the question states. (Reading the input string 1 character at a time)

  8. #23
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Your first loop has no control condition. Here, a while loop would be more appropriate.
    Here are the general guidelines on choosing a loop (though there may be exceptions):

    for loop: The most commonly used. Use the for when you know exactly how many times the loop will run.
    for example, when you want to print the string one character at a time, you know how many characters are in the string, so it will loop once for each one
    Code:
    for (i=length-1; i>=0; --i)
    {
         printf ("&#37;c", &string[i]);
    }
    Use a while loop when you don't know how many times the code will loop. When you are reading in characters, you have no idea what I will type before I hit enter. You just have to keep reading in characters until I hit enter (until you encounter the character '\n')

    The do...while loop is bottom tested, so it will run at least one time. The regular-ass while loop is top tested and it may run zero or more times. Don't forget the semicolon in the do...while loop
    Code:
    do
    { ...
    } while (condition);
    Damn... I missed a lot! I must type too slowly.
    Last edited by NeonBlack; 03-12-2008 at 01:47 AM. Reason: fatal typo
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  9. #24
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by guitarscn View Post
    I am pretty satisfied with the way the original solution came out - but NeonBlack did make a suggestion that I try to follow the instructions in the beginning, which I did not follow to get my current solution. So basically I'm trying to solve this all over again, but following the directions which the question states. (Reading the input string 1 character at a time)
    In this case you need to fix your condition of the for loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #25
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    Here is what I have so far:


    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
            int line = 0;     /* start at array block 1 */
            char count[255]; /* set a max array of 255 characters */
    
            printf("enter a line: ");
            while (line < 255)                /* start at array block 1, as long as you don't press enter, */
            {                                 /* and don't go over 255 characters, */
                    scanf("&#37;c", count[line]); /* let the user input a character on the next block (++line) */
                    ++line;                   /* and store the input character to the corresponding array block number */
            }
    
            for (line = line - 1; line >= 0; --line) /* start at the last character block inputted and go down to the first */
                    printf("%c", count[line]); /* print all characters on the whole array going backwards (--line) */
    
            printf("\n");
    
            return 0;
    }

    Still gives me a "Segmentation fault (core dumped)" error after I press Enter after my input
    Last edited by guitarscn; 03-12-2008 at 01:43 AM.

  11. #26
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Closer. I kept trying to give you this hint: When you hit enter, the newline character '\n' will be put into count[line]. So you also have to check that the character you've just read in is not '\n'. What I said about do...while was another hint. You can't check the character until after you've read it in, so I would go with the bottom tested do...while (you also need to read at least one character).
    Also, you scanf should be
    Code:
    scanf ("&#37;c", &count[line]);
    Notice the & infront of count[line] (yes, I forgot it in my example). Just remember this right now. You will understand the reason when you learn more about pointers.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  12. #27
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    Hmmm...this is what I could get


    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
            int line = 0; /* start at the first array slot */
            char count[255]; /* max 255 characters */
    
            printf("enter a line: ");
    
            do
            {
                    scanf("%c", &count[line]);       /* user enters character starting with first array slot */
                    ++line;                          /* keep going (++line) until '\n' becomes true or */
            } while (printf("\n") == 0, line < 255); /* exceed 255 characters */
    
            for (line = line - 1; line >= 0; --line) /* start at the last spot (excluding the '\n') */
                    printf("%c", count[line]);       /* output all characters backwards from each array spot (--line) */
    
            printf("\n");
    
            return 0;
    }

  13. #28
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    well, you made some good attempts.
    try
    Code:
    while (count[line-1] != '\n' && line<255);
    You see, if the character just read in was the enter key, or if we read in too many characters, the loop will break.
    I don't know if you know this yet, && is the logical AND, so both conditions must be true for the loop to continue.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  14. #29
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    Wow, that's pretty nifty! (It worked flawlessly btw).

    At this point I'm not sure if it's my lack of knowledge or the ability to think outside the box, but I wish I could have thought of that!

    It's always after seeing the solution you feel like slapping yourself on the head for not thinking of that, because you knew all the content but just couldn't recall the pieces together!
    Last edited by guitarscn; 03-12-2008 at 02:19 AM.

  15. #30
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    As you learn to program, you not only learn solutions to problems, you also learn how to think of your own solutions to new problems. Just keep at and in no time you'll look back on this and wonder how you could have been so blind to the obvious solution.
    That's all for tonight, I'm going to bed. (I haven't been able to sleep since we changed to daylight-saving time)
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. Taking input and Outputting it backwards
    By whtpirate in forum C Programming
    Replies: 9
    Last Post: 06-08-2003, 10:59 AM