Chars
chrtest.c
chrtest.c
/* File : chrtest.c
By : Tep Dobry
Date :
*/
/* This file contains a short program to illustrate the problems of
mixing numeric and character input */
#include <stdio.h>
#define FLUSH while(getchar()!='\n');
/*
#define FIX_IT
*/
main()
{ int number;
char ch = 'y';
while(ch != 'n')
{ printf("enter a number: ");
scanf("%d", &number);
/* do something with it */
printf("number is %d\n",number);
#ifdef FIX_IT
FLUSH
#endif
printf("do you wish to continue(y/n): ");
ch = getchar();
}
printf("bye\n");
}
chrutil.c
chrutil.c
/* File: chrutil.c */
/* This file contains various utility functions for processing characters */
#include <stdio.h>
#include "tfdef.h"
#include "chrutil.h"
/* Function converts ch to an integer if it is a digit. Otherwise, it
prints an error message.
*/
int dig_to_int(char ch)
{
if (IS_DIGIT(ch))
return ch - '0';
printf("ERROR:dig_to_int: %c is not a digit\n", ch);
return ERROR;
}
/* Function converts a positive integer less than 10 to a corresponding
digit character.
*/
char int_to_dig(int n)
{
if (n >= 0 && n < 10)
return n + '0';
printf("ERROR:int_to_dig: %d is not in the range 0 to 9\n", n);
return NULL;
}
/* Function reads the next integer from the input */
int getint()
{ int n = 0;
int got_dig = FALSE;
signed char ch;
ch = getchar(); /* read next char */
while (IS_WHITE_SPACE(ch)) /* skip white space */
ch = getchar();
while (IS_DIGIT(ch)) { /* repeat as long as ch is a digit */
n = n * 10 + dig_to_int(ch); /* accumulate value in n */
got_dig = TRUE;
#ifdef DEBUG
printf("debug:getint: ch = %c\n", ch); /* debug statement */
printf("debug:getint: n = %d\n", n); /* debug statement */
#endif
ch = getchar(); /* read next char */
}
if(ch == EOF) return EOF; /* test for end of file */
if(!got_dig) return ERROR; /* test for no digits read */
return n; /* otherwise return the result */
}
/* Function tests if c is an alphabetic letter. */
int letterp(char c)
{
if (IS_LOWER(c) || IS_UPPER(c))
return TRUE;
return FALSE;
}
/* Function returns TRUE if c is a delimiter, i.e., it is a white space
or a punctuation. Otherwise, it returns FALSE.
*/
int delimitp(char c)
{
if (whitep(c) || punctp(c))
return TRUE;
return FALSE;
}
/* Function returns TRUE if c is white space; returns FALSE otherwise. */
int whitep(char c)
{
if (c == '\n' || c == '\t' || c == ' ')
return TRUE;
return FALSE;
}
/* Function returns TRUE if c is a punctuation; returns FALSE otherwise. */
int punctp(char c)
{
if (c == '.' || c == ',' || c == ';' || c == ':'
|| c == '?' || c == '!')
return TRUE;
return FALSE;
}
/* Function checks if c is a vowel. */
int vowelp(char c)
{
switch(c) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': return TRUE;
default: return FALSE;
}
}
/* Function tests if c is printable. */
int illegal(char c)
{
if (IS_PRINT(c) || IS_WHITE_SPACE(c))
return FALSE;
return TRUE;
}
chrutil.h
chrutil.h
/* File: chrutil.h */
/* This file contains various macros and prototypes for character processing */
#define ERROR -2
#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
#define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
#define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
#define IS_WHITE_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
#define IS_PRINT(c) ((c) >= 32 && (c) < 127)
#define LOWER 0
#define UPPER 1
#define DIGIT 2
#define PUNCT 3
#define SPACE 4
#define CONTROL 5
#define SPECIAL 6
int dig_to_int(char ch);
char int_to_dig(int n);
char uppercase(char ch);
int getint();
int delimitp(char c);
int whitep(char c);
int punctp(char c);
int vowelp(char c);
int letterp(char c);
int illegal(char c); /* Tests if c is legal. */
count.c
count.c
/* File: count.c
By: Tep Dobry
Date:
*/
/* This is a short program to count the number of characters in a file */
#include <stdio.h>
main()
{ char ch;
int count = 0;
/* while there are more characters */
while(scanf("%c", &ch) != EOF)
/* count the character */
count = count + 1;
/* print the result */
printf("There were %d characters in the file\n",count);
}
count.dat
count.dat
There should be 65 characters
in this file
on 3 different lines.
count2.c
count2.c
/* File: count2.c
By: Tep Dobry
Date:
*/
/* This is a short program to count the number of characters in a file
using getchar() */
#include <stdio.h>
main()
{ int count = 0;
/* while there are more characters */
while(getchar() != EOF)
/* count the character */
count = count + 1;
/* print the result */
printf("There were %d characters in the file\n",count);
}
count2.dat
count2.dat
There should be 65 characters
in this file
on 3 different lines.
count3.c
count3.c
/* File: count3.c
By: Tep Dobry
Date:
*/
/* This is a short program to count the number of characters
and lines in a file using getchar() */
#include <stdio.h>
#include "chrutil.h"
main()
{ int c_count = 0;
int l_count = 0;
char ch;
/* while there are more characters */
while((ch = getchar()) != EOF)
{ /* count the character */
c_count = c_count + 1;
/* if the character is a newline - count the line */
if(ch == '\n') l_count = l_count + 1;
}
/* print the results */
printf("There were %d characters in the file\n",c_count);
printf("There were %d lines in the file\n",l_count);
}
count3.dat
count3.dat
There should be 65 characters
in this file
on 3 different lines.
wds.c
wds.c
/* Program File: wds.c
Other Source Files: chrutil.c
Header Files: tfdef.h, chrutil.h
This program reads standard input characters and prints each word on a
separate line. It also counts the number of lines, words, and characters.
All characters are counted including the newline and other control
characters, if any.
*/
#include <stdio.h>
#include "tfdef.h"
#include "chrutil.h"
#define INTERACT
/*
*/
main()
{ signed char ch;
int inword, /* flag indicating when in a word */
lns, wds, chrs; /* Counters for lines, words, chars. */
#ifdef INTERACT
printf("***Line, Word, Character Count Program***\n\n");
printf("Type characters, EOF to quit\n");
#endif
lns = wds = chrs = 0; /* initialize counters to 0 */
inword = FALSE; /* before beginning we are not in a word */
while ((ch = getchar()) != EOF) /* while there are more characters */
{ chrs = chrs + 1; /* count characters */
if (ch == '\n') /* if newline char */
lns = lns + 1; /* count lines */
/* if not in a word and not a delimiter */
/* then this must be the beginning of a word */
if (!inword && !delimitp(ch)) /* if not in word and not delim. */
{ inword = TRUE; /* remember we are in a word */
wds = wds + 1; /* count words */
}
/* otherwise if in a word, but found a delimiter */
/* then we just went beyond the end of the word */
else if (inword && delimitp(ch)) /* if in word and a delimiter*/
{ inword = FALSE; /* we are no longer in a word*/
putchar('\n'); /* end word with a newline */
}
if (inword) /* if in a word */
putchar(ch); /* print the character */
}
/* print the results */
printf("Lines = %d, Words = %d, Characters = %d\n",
lns, wds, chrs);
}