Thread: Printf statement within a decrementing for loop does not print correctly

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

    Printf statement within a decrementing for loop does not print correctly

    Code:
    int main(int argc, const char * argv[]) {
        
        char str[79];
        char reverse[79][79];
        char *ptr;
        
        int i = 0;
        printf("Input: ");
        
        
        fgets(str,79,stdin);
        ptr = strtok(str," ");
        
        
            while (ptr !=NULL){
            
            strcpy(reverse[i],ptr);
            i++;
            ptr = strtok(NULL," ");
            }
        
        
          for(i=2;i>=0;i--) {
            printf(" %s",reverse[i]);
        }
        
        
        return 0;
    }
    Basically my program will reverse words.
    for example
    input:bees and birds
    output: birds and bees

    However, somehow my for loop skips a line after i=0

    input: birds and bees
    output: birds <------ After 1st world, a line is skipped for no reason.
    andbees


    how does this happen? it seems impossible.
    Last edited by tmac619619; 08-23-2015 at 02:08 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I can't reproduce the problem. C code - 33 lines - codepad

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by whiteflags View Post
    I can't reproduce the problem. C code - 33 lines - codepad

    Hi!

    Yes your code is perfect! I'm comparing mines with yours right now... but what seems off?


    my output

    Code:
    input: birds and bees
    output: bees
    andbirds

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Nevermind. I figured it out.

    You need to remove the newline at the end of the string before you do the reversing.

    That can be done pretty simply with a small branch like this:
    Code:
    char *newline = strchr(str, '\n');
    if (newline != NULL)
       *newline = '\0';
    Something like that would remove the newline in the output.

    What is really hard to explain is why "andbirds" is squished together in the output.
    Last edited by whiteflags; 08-23-2015 at 03:33 PM.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Got it!

    Thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-05-2015, 10:57 AM
  2. Am I doing this homework problem correctly? (Switch Statement)
    By Cameron Taylor in forum C Programming
    Replies: 5
    Last Post: 06-17-2013, 08:04 PM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. decrementing for loop help needed
    By adamuk in forum C Programming
    Replies: 5
    Last Post: 01-17-2006, 02:29 PM
  5. how to print .03 as 3% in printf?
    By shiyu in forum C Programming
    Replies: 2
    Last Post: 01-31-2003, 12:02 PM