Need help to understand if else statement in my code.

Started by Shikatsu, March 20, 2011, 07:48:32 PM

Previous topic - Next topic

Shikatsu

Greetings EveryOne :D

* Exercise: Write a program that prints its input one word per line.

This is the code that i came up with:


#include <stdio.h>

int main()
{
   int c;

   while((c = getchar()) != EOF)
   {
       if(c != ' ' || c != '\n' || c != '\t')
       {
           putchar(c);
       }
       else
           if(c == ' ' || c == '\n' || c == '\t')
           {
               putchar('\n');
           }
   }
}


the upper code is not returning the result i seek, but the next one does:


#include <stdio.h>

int main()
{
   int c;

   while((c = getchar()) != EOF)
   {
       if(c != ' ' || c != '\n' || c != '\t')
       {
           putchar(c);
       }

       if(c == ' ' || c == '\n' || c == '\t')
       {
           putchar('\n');
       }
   }
}


Can someone tell me please why "if else" is not an answer for the exercise?

Thank You In Advance ;D

AlexN

The first line is the problem. The result of the condition will be each time true.

You should use the first line in this way:
if (!(c == ' ' || c == '\n' || c == '\t'))
... and then you can throw away the second if().

But I would change the then and else part and write the code in this way:

if(c == ' ' || c == '\n' || c == '\t')
{
    putchar('\n');
}
else
{
    putchar(c);
}
best regards
Alex ;)

Shikatsu


AlexN

Quote from: Shikatsu on March 20, 2011, 09:13:21 PM
Thank you AlexN, but i still dont get it.
If c==' ' then will be c != '\n' and c != '\t' true and you or the single results - so the complete result will be true. The same is for c=='\n' and c=='\t' and if the content of c is any other charater the result of all three conditions are true. How ever the complete result will be true in each case,
best regards
Alex ;)

frankie

#4
Even simpler, you want to write the char
    if it is not <space> *and* not <tab> *and* not <newline>
        then print it
    else
        jump to new line
The logical operator that is true only when all its input conditions are true is the *and* operator. Using the 'or' if only one condition is true the output is true.
Now, as AlexN already said, if your input is one of the three spacing codes the other two conditions will be true anyway. So you will never reach the else.
Moreover a comparison is a binary branch meaning that you have two directions you can follow: one if the 'if expression' is true and the other if it false. So you don't need to add another if after else to check again the reverse result.
I suggest you to review logical operators and boolean algebra. (Maybe you want apply the 'break the bar and change the sign' rule to AlexN's proposed 'if (!(c == ' ' || c == '\n' || c == '\t'))' to verify that it is exactly the same reported below using the 'and' operator).
The correct code is simply:
#include <stdio.h>

int main()
{
    int c;

    while((c = getchar()) != EOF)
    {
        if(c != ' ' && c != '\n' && c != '\t')
            putchar(c);
        else
            putchar('\n');
    }
}

(The brackets around the if are unnecessary because you have only one statement, and also the brackets of the 'while' conditions would be unnecessary because you have only the 'if' block)
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Shikatsu

Thank you frankie and AlexN for the help, i got it now.
the problem i wasn't thinking clearly my head was fuzzy ^^