Load and display the next program, charclas.c for an example of character counting. We have repeatedly used the backslash n character representing a new line. There are several others that are commonly used, so they are defined in the following table;
\n Newline
\t Tab
\b Backspace
\" Double quote
\\ Backslash
\0 NULL (zero)
By preceding each of the above characters with the backslash character, the character can be included in a line of text for display, or printing. In the same way that it is perfectly all right to use the letter "n" in a line of text as a part of someone’s name, and as an end-of-line, the other characters can be used as parts of text or for their particular functions. The program on your screen uses the functions that can determine the class of a character, and counts the characters in each class. The number of each class is displayed along with the line
itself. The three functions are as follows;
isalpha(); Is the character alphabetic?
isdigit(); Is the character a numeral?
isspace(); Is the character any of, \n, \t, or blank?
#include "/sys/stdio.h"
#include "/sys/ctype.h" /*note your compiler may not need this */
main( )
{
FILE *fp;
char line[80],filename[24];
char *c;
printf("Enter filename -> ");
scanf("%s",filename);
fp = fopen(filename,"r");
do {
c = fgets(line,80,fp); /* get a line of text */
if (c != NULL) {
count_the_data(line);
}
} while (c != NULL);
fclose(fp);
}
count_the_data(line)
char line[];
{
int whites, chars, digits;
int index;
whites = chars = digits = 0;
for (index = 0;line[index] != 0;index++) {
if (isalpha(line[index])) /* 1 if line.. is alphabetic */
chars++;
if (isdigit(line[index])) /* 1 if line.. is a digit */
digits++;
if (isspace(line[index])) /* 1 if line.. is blank, tab, */
whites++; /* or newline */
} /*end of counting loop */
printf("%3d%3d%3d %s",whites,chars,digits,line);
}
This program should be simple for you to find your way through so no explanation will be given. It was necessary to give an example with these functions used.
Compile and run this program with any file you choose. See also dosex1616.c in Chapter 14 for another example.
\n Newline
\t Tab
\b Backspace
\" Double quote
\\ Backslash
\0 NULL (zero)
By preceding each of the above characters with the backslash character, the character can be included in a line of text for display, or printing. In the same way that it is perfectly all right to use the letter "n" in a line of text as a part of someone’s name, and as an end-of-line, the other characters can be used as parts of text or for their particular functions. The program on your screen uses the functions that can determine the class of a character, and counts the characters in each class. The number of each class is displayed along with the line
itself. The three functions are as follows;
isalpha(); Is the character alphabetic?
isdigit(); Is the character a numeral?
isspace(); Is the character any of, \n, \t, or blank?
#include "/sys/stdio.h"
#include "/sys/ctype.h" /*note your compiler may not need this */
main( )
{
FILE *fp;
char line[80],filename[24];
char *c;
printf("Enter filename -> ");
scanf("%s",filename);
fp = fopen(filename,"r");
do {
c = fgets(line,80,fp); /* get a line of text */
if (c != NULL) {
count_the_data(line);
}
} while (c != NULL);
fclose(fp);
}
count_the_data(line)
char line[];
{
int whites, chars, digits;
int index;
whites = chars = digits = 0;
for (index = 0;line[index] != 0;index++) {
if (isalpha(line[index])) /* 1 if line.. is alphabetic */
chars++;
if (isdigit(line[index])) /* 1 if line.. is a digit */
digits++;
if (isspace(line[index])) /* 1 if line.. is blank, tab, */
whites++; /* or newline */
} /*end of counting loop */
printf("%3d%3d%3d %s",whites,chars,digits,line);
}
This program should be simple for you to find your way through so no explanation will be given. It was necessary to give an example with these functions used.
Compile and run this program with any file you choose. See also dosex1616.c in Chapter 14 for another example.

