0

、、、

#include <stdio.h>
#include <ctype.h>   // for isspace()
#include <stdbool.h> // for bool, true, false
#define STOP '|'
int main(void)
{
    char c;              // read in character
    char prev;           // previous character read
    long n_chars = 0L;   // number of characters  
    int n_lines = 0;     // number of lines
    int n_words = 0;     // number of words
    int p_lines = 0;     // number of partial lines
    bool inword = false; // == true if c is in a word
    printf("Enter text to be analyzed (| to terminate):\n");
    prev = '\n'; // used to identify complete lines
while ((c = getchar()) != STOP)
    { 
        if (c == '\n') //
            n_lines++; // count lines
        else if (c == '\t')
            n_chars+=4;
        else
            n_chars++;
        if (!isspace(c)&&c>='a'&&c<='z'&& !inword) 
        {
            inword = true; // starting a new word
            n_words++;     // count word
        }
        if (isspace(c) && inword) // 
            inword = false; // reached end of word
        prev = c;           // save character value
}
if (prev != '\n') p_lines = 1;
printf("characters = %ld, words = %d, lines = %d, ",
n_chars, n_words, n_lines);
printf("partial lines = %d\n", p_lines);
return 0;
} // end of main

、、、

this is a program that calculate the input's characters,words(treat a group of letters as words),and lines in my program environment a row can only contain 160 characters if the input is more than that it will automatically go to next row .Is it add the \n automatically? and if its not how can i calculate this kind of row?

tree
  • 13
  • 3
  • Use `islower(c)` instead of `c >= 'a' && c <= 'z'` – William Pursell Nov 28 '21 at 18:47
  • 4
    I don't understand your question, but I'm pretty sure the answer is "no". C doesn't do very much "automatically", and it certainly isn't going to insert data for you. – William Pursell Nov 28 '21 at 18:49
  • @WilliamPursell, well in some systems it *might* insert a `\r` before each `\n`. – HAL9000 Nov 28 '21 at 19:14
  • I think their question is about the behavior of the terminal. Yes if you fill up a row with characters then you end up at the beginning of the next row. But no, this is not done by the program (outputting a newline character), but it is done by the terminal. – Crouching Kitten Nov 28 '21 at 19:24
  • About this: `how can i calculate this kind of row?` You have to use a terminal library, which tells you the row size. – Crouching Kitten Nov 28 '21 at 19:25
  • @Crouching Kitten can i terminal library to get the hole input characters' size or just characters' size in a row? and is it impossible to calculate this kind of row in the c program? – tree Nov 29 '21 at 06:18
  • @tree Use this: https://man7.org/linux/man-pages/man2/ioctl.2.html Like `struct winsize size; ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);`. Then the row width is in `size.ws_col`. The size of the characters? That's not possible with this library. Maybe this: https://stackoverflow.com/questions/22782703/how-to-get-terminal-size-or-font-size-in-pixels – Crouching Kitten Nov 29 '21 at 09:26

0 Answers0