Thread: Help with program clearing white space.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    12

    Help with program clearing white space.

    Here is my code...

    Code:
    /*
    */
    
    
    #include <stdio.h>
    #include <conio.h>
    
    
    void fix(char []);
    int main()
    {
       char str1[81];
       
     
       printf("Enter text\n");
       gets(str1);
       fix(str1);
     
      
     
       getch();
       return 0;
       
    }
    void fix(char str1[])
    {
         char str2[81];
         int c = 0, d = 0; 
          while (str1[c] != '\0')
       {
       if (!(str1[c] == ' ' && str1[c+1] == ' ')) 
          {
          str2[d] = str1[c];
          d++;
          }
          c++;
       }
     
       str2[d] = '\0';
     
       printf("The Corrected Text is: \n%s\n", str2);
    }
    Two things. Would there be a better way to do this without using anything other than stdio.h. Also, can someone please explain to me what this portion of the code does.

    Code:
     if (!(str1[c] == ' ' && str1[c+1] == ' '))
    the "!" is throwing me off. I know what the if statement is doing but when i went to the book i cant find much on the "!" other than i needed it to make my statement work.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Would there be a better way to do this without using anything other than stdio.h.
    "conio.h" is just used in this program for the "getch()". Whether or not there's legitimate uses for it, it is just used here to stall the window from closing. A standard version for this might be "getchar()" which would allow you to get rid of the "conio.h."

    Also, can someone please explain to me what this portion of the code does.
    The "!=" (not-equal-to) operator is the complement of the "==" (equal-to) operator.

    In its most basic logical form, it can be expressed as thus:

    Code:
    true == true
    false == false
    true != false
    false != true
    In C, it is used mostly for comparison:

    Code:
    int numberCarrots = 2;
    
    if(numberCarrots == 2)
        printf("This will print\n");
    if(numberCarrots != 2)
        printf("This will not print\n");
    I can break it down further for you, but I want to see if you get the general idea first.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    i understand what != is and == but this is what i dont understand .
    Code:
     if(!(str1[c] == ' '&& str1[c+1] == ' '))
    the (!(str1 is whats throwing me off. i know im asking that if str1 has a space then do this. but the if(! is what i dont get.
    Last edited by chumpp; 07-31-2012 at 10:07 PM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Let me ask you this - do you work well with math? What would be the value of 'x' here:

    x = 6 + 6 - 6 x 0

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    Quote Originally Posted by Matticus View Post
    Let me ask you this - do you work well with math? What would be the value of 'x' here:

    x = 6 + 6 - 6 x 0
    it would be 0 if your asking 6+6-6 * 0

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you take order of operations into account, it would actually be 12. You need to understand that before we proceed.

    [edit] my apologies on using 'x' for a variable and multiplication sign - you had the right idea in your reply [/edit]
    Last edited by Matticus; 07-31-2012 at 10:35 PM.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    ops sorry yes it would be 12. x = 6 + 6 - 6 * 0 the 6 * 0 would be first then the rest would just go from left to right unless we had ()

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Correct. Now, in that example, order of operations tells use we have to do the multiplication first. So you could imagine it written like this:

    Code:
    x = 6 + 6 - 6 x 0
    x = (6 + 6 - (6 x 0))
    x = (6 + 6 - 0)
    x = 6 + 6
    x = 12
    The parenthesis help us see what we're supposed to figure out first. You use this same idea in programming - break down the statement bit by bit, starting with the expressions in the inner-most parenthesis first:

    Code:
    if(!(str1[c] == ' '&& str1[c+1] == ' '))
        |                                 |
        |<--                           -->|
          if "str1[c]" AND "str1[c+1]" are
          both a space, this part is TRUE
    
    if(!(TRUE))
       |     |
       |<--->|
         NOT TRUE is FALSE
    
    if(FALSE)
        // don't execute code
    
    The "execute code" will execute if the expression is not FALSE.
    The "if()" statement will execute if the two elements of the array don't both contain space characters.
    Last edited by Matticus; 07-31-2012 at 11:06 PM. Reason: clarification

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    ah ok i see now. so the code
    Code:
    if(!(TRUE))
    is saying not true then execute code correct?

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    My apologies, I clarified my previous post.

    Code:
    if(1)  // non-zero is "true"
        printf("This will print\n");
    if(0)  // zero is "false"
        printf("This will not print\n");
    if(NOT TRUE) means it won't execute the code

    Code:
    if(!1) == if(0) == code in brackets beneath won't execute.

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    EDIT! ok i get it now. let me recap to make sure.

    Code:
    if(!(str1[c] == ' '&& str1[c+1] == ' '))
    so its going to test if both have spaces and if it does then its true. then its saying if(!(true) is saying not true is false then do the following code.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "not true" is false, so "if(not true)" is "if(false)" is don't do the following code.

    If they're not both spaces, then you have a "not false" which is "true" which means the following lines will execute.

    Sorry, didn't sound this confounding in my head =)

  13. #13
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    i see. ya i got the basics of everything and im doing well in my classes its just for some reason my brain wont let me understand "!" i see what your saying its just a bit confusing. I dont like writing certain code if i dont know what is exactly going on in my program. Would there be a way to write this program and not use the (!(TRUE)?

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Yes, several ways. When you can identify them, you know you've got the grasp of the basics.

    Try making a "practice" file (I call mine "scrappad.c") that you can open and just throw some basic code into to actually see what's happening.

    Code:
    #include <stdio.h>
    
    #define TRUE 1
    #define FALSE 0
    
    int main(void)
    {
        if(TRUE)
            printf("true\n");  // <-- will print
        if(FALSE)
            printf("false\n");  // <-- will not print
        if(!FALSE)
            printf("not false is true\n");  // <-- will print
        return 0;
    }
    You could also try De Morgan's Laws, but while effective, they're no less difficult:

    Code:
    not (A and B) == (not A) or (not B)

  15. #15
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    ok now i understand! this made it much more clear.
    Code:
    if(!FALSE)
    printf("not false is true\n"); // <-- will print
    Thanks so much! this really did help me.
    Last edited by chumpp; 08-01-2012 at 12:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings and white space
    By ginom71 in forum C Programming
    Replies: 13
    Last Post: 07-12-2009, 09:48 AM
  2. remove white space
    By pokemon in forum C Programming
    Replies: 12
    Last Post: 03-10-2009, 03:12 PM
  3. white space and fscanf
    By DMaxJ in forum C Programming
    Replies: 2
    Last Post: 06-10-2003, 09:18 AM
  4. File I/O (white-space)
    By yougowego in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 10:35 PM