、、、
#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?