Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to figure out the logical error in C program

A program that prints its input one word per line.

int main() {

    int c;

    while ((c=getchar()) != EOF) {

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

The above program prints the result correctly, one word per line. After changing the condition accordingly, I was expecting the program below to also print one word per line. However I am not getting the correct result. Am I making some silly mistake or is something wrong?

int main() {

    int c;

    while ((c=getchar()) != EOF) {

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

    return 0;

}
like image 676
bornfree Avatar asked May 16 '11 20:05

bornfree


People also ask

How do I find logic errors in code?

The program will execute the code but will produce unexpected results. Logic errors are usually resolved by carrying out a dry run, using a trace table or setting breakpoints to help identify the section of code that contains the logic error.

What are logical errors in C programming?

Logical errors are those errors in which we think that our code is correct, the code compiles without any error and gives no error while it is running, but the output we get is different from the output we expected.

What is debugging explain the way to find out logical errors?

Definition: Debugging is the process of detecting and removing of existing and potential errors (also called as 'bugs') in a software code that can cause it to behave unexpectedly or crash. To prevent incorrect operation of a software or system, debugging is used to find and resolve bugs or defects.


3 Answers

the correct change of condition is:

if (!(c == ' ' || c == '\n' || c == '\t'))

or

if (c != ' ' && c != '\n' && c != '\t')

See De Morgan's law

like image 188
MByD Avatar answered Sep 19 '22 15:09

MByD


You've gotten a number of answers to your original question, but I feel obliged to add one minor detail: both the original and the modified version suffer from a couple of what I'd consider problems. First, they don't really detect white-space correctly (e.g., they ignore vertical tabs, and any other white-space as defined by the locale), and they produce blank lines if words are separated by more than one white-space character.

For the first problem, I'd use isspace instead of directly comparing to the white-space characters you know about (incidentally, eliminating the source of the problem you encountered). For the second, you could add some logic to skip all consecutive white-space characters when you encounter the first, or you could add a flag to write out a new-line if and only if the current character is a space and the previous character you wrote was not a new-line.

Alternatively, you could read words using scanf with the %s conversion:

char buffer[256];

while (scanf("%255s", buffer))
    printf("%s\n", buffer);

This approach, however, imposes an upper limit on the size of a single word. Under normal circumstances, that's rarely problem, but depending on the nature of the input it could/can be.

like image 29
Jerry Coffin Avatar answered Sep 19 '22 15:09

Jerry Coffin


You need to change the ||s to &&s, i.e. change

        if (c != ' ' || c != '\n' || c != '\t')

to

        if (c != ' ' && c != '\n' && c != '\t')

i.e. "IF c not equal to space AND c not equal to return AND c not equal to tab THEN ..."

like image 23
Paul R Avatar answered Sep 19 '22 15:09

Paul R