I have a question about C language bowl

Asked 1 years ago, Updated 1 years ago, 105 views

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#define STOP '.'

int main()
{
    char c;
    int n_chars = 0;        // number of non-space characters
    int n_lines = 0;
    int n_words = 0;
    bool word_flag = false;
    bool line_flag = false;

    printf("Enter text : \n");

    while ((c = getchar()) != STOP)
    { 
        If (!isspace(c)) // count non-space characters.. if the entered character is not blank 
            n_chars++;      

        If (!isspace(c) &&!line_flag) // if line_flag is false, not blank 
        {
            n_lines++;
            line_flag = true;
        }
        if (c == '\n')
            line_flag = false;

        if (!isspace(c) && !word_flag)
        {
            n_words++;
            word_flag = true;
        }
        if (isspace(c))
            word_flag = false;

    }
    printf("Characters = %d, Words = %d, Lines = %d\n", n_chars, n_words, n_lines);

    return 0;
}

I don't understand from if(!isspace(c) &&!line_flag) in this code I can't understand even if I try to debug it.

I initialized line_flag to false but if(!isspace(c) &&!line_flag is !line_flag in statement? It's successful to transform it into another example, but I don't understand this example itself. Help me.

c boolean

2022-09-20 15:40

1 Answers

I initialized line_flag to false, but !line_flag is false or true?

true. In general, FOO is evaluated as the opposite of the non-verbal evaluation value of FOO.

If you're confused, try changing the name of the flag variable. For example, if you replace line_flag with something like should_start_new_line, it might be easier to understand what !should_start_new_line means by reading it carefully.


2022-09-20 15:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.