Skip to content

Instantly share code, notes, and snippets.

@kunpengku
Created May 26, 2013 07:45
Show Gist options
  • Save kunpengku/5652003 to your computer and use it in GitHub Desktop.
Save kunpengku/5652003 to your computer and use it in GitHub Desktop.
wordcount
#include <stdio.h>
#include <ctype.h>
#define STOP '|'
int main(void)
{
char c;
char prev;
long n_chars = 0L;
int n_lines = 0;
int n_words = 0;
int p_lines = 0;
int inword = 0;
printf("Enter text to be analyzed(| to terminate):");
prev = '\n';
while((c = getchar()) != STOP)
{
n_chars++;
if(c == '\n')
n_lines++;
if(!isspace(c) && inword == 0)
{
inword = 1;
n_words++;
}
if(isspace(c) && inword == 1)
inword = 0;
prev = c;
}
if(prev != 'n')
p_lines++;
printf("character = %ld,words = %d,lines = %d,partial lines = %d",n_chars,n_words,n_lines,p_lines);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment