Thread: i understood the code. expect one thing

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    i understood the code. expect one thing

    //if you wrote a string with a number of spaces then this code reduce it to one space only between words.
    i marked with a comment the part i didnt understood.

    #include <stdio.h>
    Code:
     
    int main()
    {
       char text[100], blank[100];
       int c = 0, d = 0;
     
       printf("Enter some text\n");
       gets(text);
     
       while (text[c] != '\0')
       {
          if (!(text[c] == ' ' && text[c+1] == ' ')) {
            blank[d] = text[c];
            d++; // here, why the program crashes if the c++ were put here.
    }              //what is the difference.
        c++;   //c++ here works.
       }
     
    blank[d] = '\0';
     
       printf("Text after removing blanks\n%s\n", blank);
     
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You've been told before to post with proper indentation. Please don't make us tell you again. Properly formatted code makes it much easier to follow the logic, find bugs, etc. There's a way to preview your post, so you can see if it's properly formatted, and if not edit it to fix. For example, this is properly indented
    Code:
    #include <stdio.h>
    
    int main()
    {
        char text[100], blank[100];
        int c = 0, d = 0;
    
        printf("Enter some text\n");
        gets(text);
    
        while (text[c] != '\0')
        {
            if (!(text[c] == ' ' && text[c+1] == ' ')) {
                blank[d] = text[c];
                d++; // here, why the program crashes if the c++ were put here.
            }              //what is the difference.
            c++;   //c++ here works.
        }
    
        blank[d] = '\0';
    
        printf("Text after removing blanks\n%s\n", blank);
    
        return 0;
    }
    Also, don't use gets. Read this link: FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com. Instead, use fgets and trim the newline; see FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com.

    You could try printing out useful information at each step in your program. For example, every time through the loop, print the value of c and text[c].

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    26
    Quote Originally Posted by anduril462 View Post
    You've been told before to post with proper indentation. Please don't make us tell you again. Properly formatted code makes it much easier to follow the logic, find bugs, etc. There's a way to preview your post, so you can see if it's properly formatted, and if not edit it to fix. For example, this is properly indented
    Code:
    #include <stdio.h>
    
    int main()
    {
        char text[100], blank[100];
        int c = 0, d = 0;
    
        printf("Enter some text\n");
        gets(text);
    
        while (text[c] != '\0')
        {
            if (!(text[c] == ' ' && text[c+1] == ' ')) {
                blank[d] = text[c];
                d++; // here, why the program crashes if the c++ were put here.
            }              //what is the difference.
            c++;   //c++ here works.
        }
    
        blank[d] = '\0';
    
        printf("Text after removing blanks\n%s\n", blank);
    
        return 0;
    }
    Also, don't use gets. Read this link: FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com. Instead, use fgets and trim the newline; see FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com.

    You could try printing out useful information at each step in your program. For example, every time through the loop, print the value of c and text[c].
    Whats are the differences from my code and what you wrote? What do you mean by saying that the code is not easy to follow?
    Im trying to understand..thanks

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Whats are the differences from my code and what you wrote? What do you mean by saying that the code is not easy to follow?
    The only difference between what you wrote and what andruil462 posted is that the code is now formatted. In particular the brace that would logically close the if brace is flush on a different line with where the if actually started on the screen. The concern is that you would lose track of where the code under the if actually ends... the actual space that you use doesn't matter; one of the only markers that the C language pays attention to is the curly brace. You should always place them in such a way that it helps people understand what logic affects what statements. He corrected you because you needed a reminder about that.

    He didn't correct you, because he thought that you would understand if you thought about the code, with the understanding that the d++; would be executed after the if code. The reason it appears to matter is because the c is used as an index for one array and the d is used for another one.

    Here is some output from my debugger that may help. From right when your loop starts examining the string.
    Code:
    (gdb) print text[c]
    $1 = 72 'H'
    (gdb) print text[c+1]
    $2 = 101 'e'
    (gdb) n
    14                  blank[d] = text[c];
    (gdb) n
    15                  d++; // here, why the program crashes if the c++ were put he                                                                                                               re.
    (gdb) print c
    $3 = 0
    (gdb) print d
    $4 = 0
    (gdb) n
    17              c++;   //c++ here works.
    (gdb) print d
    $5 = 1
    (gdb) print c
    $6 = 0
    (gdb) n
    11          while (text[c] != '\0')
    (gdb) print c
    $7 = 1
    Do you see when c and d changed and do you understand why?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    notehowmucha rderiti stoundrestandthiswi thoutproperspacing
    The above says "Note how much harder it is to understand this without proper spacing". And it's true. Imagine reading a whole paragraph or a whole book like that, having to guess where each word starts and ends, where a new sentence, a new idea starts, etc. Without the proper spacing, it's difficult -- the same goes for source code. If you don't write your code with proper "spacing", i.e. indentation and formatting, then it's more difficult to follow and it's more likely that you will make a mistake and not see it.

    I should have been slightly more clear about my suggestion: print c and text[c] at the top of the loop in your original program. Now change d++ to c++ and try it again. Note the values of c when the loop stops.

    This also begs the question: why were you trying to change d++ to c++? Do you understand what c and d represent, what their purpose is in the program?

  6. #6
    Registered User
    Join Date
    Aug 2014
    Posts
    26
    Yes.
    Thank you both

  7. #7
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Code:
    void remove_spaces(char* dst, const char* src)
    {
        int previous_symbol_was_space = 0;
        int c;
        while( c = (unsigned char) *src++ ) {
            if( previous_symbol_was_space && c == ' ' ) { continue; }
            *dst++ = c;
            previous_symbol_was_space = c == ' ';
        }
        *dst = '\0';
    }
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. This code is not performing what I expect...
    By XenoReseller in forum C Programming
    Replies: 2
    Last Post: 12-25-2011, 10:50 PM
  2. I thought I understood this.
    By somekid413 in forum C Programming
    Replies: 2
    Last Post: 01-11-2010, 08:32 PM
  3. Have I understood the atoi funtion right?
    By austra in forum C Programming
    Replies: 5
    Last Post: 11-13-2009, 09:15 AM
  4. Functions are Still Not Understood.
    By errigour in forum C Programming
    Replies: 6
    Last Post: 04-09-2009, 02:54 PM
  5. this code doesn't print any thing with gcc ??
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 11:06 AM