Thread: homework help!!

  1. #1
    Unregistered
    Guest

    homework help!!

    I need to change this program into pointer arithmetic

    I started doing it and I just want to make sure that I have not left out someting!!!!!!



    #include <stdio.h>

    int main(void)
    {
    char t[20];
    char buf[20];
    char *pt;
    int k;

    for(;
    {
    gets(t);
    if (feof(stdin))break;
    sscanf(t,"%s", &buf);

    pt=t;
    while(strlen(pt)-1; k>=0; k--)
    {
    printf("%c", *pt[k]);
    }
    printf("\n");
    }
    return 0;
    }

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Let's see.

    1. #include <string.h> isn't there.
    2. for(; << what do want to do with this?
    3. if (feof(stdin)) I don't think you meant that.
    4. sscanf(t,"%s", &buf); is unnecessary.
    5. while(strlen(pt)-1; k>=0; k--) makes no sense
    6. printf("%c", *pt[k]); makes no sense

    1. Add #include <string.h>
    2. Complete the for loop
    3. if (feof(stdin)) Rather do if(t[0] == '\n')
    4. while(strlen(pt)-1; k>=0; k--) Do this : while(*(pt++) != '\0')
    5. printf("%c", *pt[k]); change to "printf("%c", *pt);"

  3. #3
    Unregistered
    Guest
    sorry here is my updated codes

    Why do I need string.h?

    if (feof(stdin)) break; I need that to stop the program from its endless loop.

    This program prints a string backward


    #include <stdio.h>

    int main(void)
    {
    char t[20];
    char buf[20];
    char *pt;
    int k;


    for(;
    {
    gets(t);
    if (feof(stdin))break;
    sscanf(t,"%s", &buf);

    pt=t;
    while(strlen(pt)-1; k>=0; k--)
    {
    printf("%c", *pt[k]);
    }
    printf("\n");
    }
    return 0;
    }

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Why do I need string.h?

    It's required for the string-functions you are using.

    >if (feof(stdin)) break; I need that to stop the program from its
    >endless loop.

    Not necessarily. More elegant would be to have an if-else construction and a variable which indicates if the loop should end.

    Code:
    while (... && stop == FALSE)
    {
        if (feof (stdin))
        {
            stop = TRUE;
        }
        else
        {
            ...
        }
    }
    >while(strlen(pt)-1; k>=0; k--)

    Hmmm. I suggest you read you're C book again and especially the chapters about while-loops and for-loops.

  5. #5
    Unregistered
    Guest
    sorry this is a for loop not a while loop

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I see. Still any questions?

  7. #7
    Unregistered
    Guest
    how can i change the for loop into pointer arithmetic??

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You mean this while-loop which is in fact a for-loop?

    >while(strlen(pt)-1; k>=0; k--)

    Could be something like this. (Not tested)

    Code:
    /*First store length of t in a variable. */
    t_len = strlen (t) - 1;
    
    /* Let pt point to end of t and then move backwards. Loop ends when start of t is reached. */
    for (pt = t + t_len; pt >= t; pt--)
    {
        ...
        printf ("%c", *pt);
        ...
    }

  9. #9
    Unregistered
    Guest
    Thanks shiro!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM