[ Pobierz całość w formacie PDF ]

/* case, and all lower case to upper case. It ignores all */
/* other characters. */
char line[ ];
{
int index;
for (index = 0;line[index] != 0;index++) {
if (isupper(line[index])) /* 1 if upper case */
line[index] = tolower(line[index]);
else {
if (islower(line[index])) /* 1 if lower case */
line[index] = toupper(line[index]);
}
}
printf("%s",line);
}
It should be no problem for you to study this program on your own and
understand how it works. The four functions on display in this program
are all within the user written function, mix_up_the_chars. Compile and
run the program with data of your choice. The four functions are:
isupper( ); Is the character upper case?
islower( ); Is the character lower case?
toupper( ); Make the character upper case.
tolower( ); Make the character lower case.
Many more classification and conversion routines are listed in your C
compiler s Reference Guide.
Page 813-41
Module 815 Data Structures Using C
Classification of characters
We have repeatedly used the backslash n (\n) character for representing
a new line. Such indicators are called escape sequences, and some of
the more commonly used are defined in the following table:
\n Newline
\t Tab
\b Backspace
\ Double quote
\\ Backslash
\0 NULL (zero)
A complete list of escape sequences available with your C compiler are
listed in your C Reference Guide.
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 next program [CHARCLAS.C] uses the functions that can
determine the class of a character, and counts the characters in each
class.
#include "stdio.h"
#include "ctype.h"
void count_the_data(line)
void main( )
{
char line[80]
char *c;
do {
c = gets(line); /* get a line of text */
if (c != NULL) {
count_the_data(line);
}
} while (c != NULL);
}
void 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]))
Page 813-42
Module 815 Data Structures Using C
/* 1 if line[ ] is blank, tab, or newline */
whites++;
} /* end of counting loop */
printf("%3d%3d%3d %s", whites, chars, digits, line);
}
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?
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 suitable input
data.
Logical functions
The functions in this group are used to do bitwise operations, meaning
that the operations are performed on the bits as though they were
individual bits. No carry from bit to bit is performed as would be done
with a binary addition. Even though the operations are performed on a
single bit basis, an entire byte or integer variable can be operated on in
one instruction. The operators and the operations they perform are
given in the following table:
& Logical AND, if both bits are 1, the result is 1.
| Logical OR, if either bit is one, the result is 1.
^ Logical XOR, (exclusive OR), if one and only one
bit is 1, the result is 1.
~ Logical invert, if the bit is 1, the result is 0, and if
the bit is 0, the result is 1.
The following example program [BITOPS.C] uses several fields that
are combined in each of the ways given above.
#include "stdio.h"
void main( )
{
char mask;
char number[6];
char and,or,xor,inv,index;
number[0] = 0X00;
number[1] = 0X11;
number[2] = 0X22;
number[3] = 0X44;
number[4] = 0X88;
number[5] = 0XFF;
printf(" nmbr mask and or xor inv\n");
mask = 0X0F;
for (index = 0; index
Page 813-43
Module 815 Data Structures Using C
and = mask & number[index];
or = mask | number[index];
xor = mask ^ number[index];
inv = ~number[index];
printf("%5x %5x %5x %5x %5x %5x\n", number[index],
mask, and, or, xor, inv);
}
printf("\n");
mask = 0X22;
for (index = 0; index
and = mask & number[index];
or = mask | number[index];
xor = mask ^ number[index];
inv = ~number[index];
printf("%5x %5x %5x %5x %5x %5x\n", number[index],
mask, and, or, xor inv);
}
} [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • cukierek.xlx.pl