预备知识:
1. 头文件:<cctype>
isalnum()
checks for an alphanumeric character; it is equivalent to (isal‐
pha(c) || isdigit(c)).
isalpha()
checks for an alphabetic character; in the standard "C" locale,
it is equivalent to (isupper(c) || islower(c)). In some
locales, there may be additional characters for which isalpha()
is true—letters which are neither upper case nor lower case.
isascii()
checks whether c is a 7-bit unsigned char value that fits into
the ASCII character set.
isblank()
checks for a blank character; that is, a space or a tab.
iscntrl()
checks for a control character.
isdigit()
checks for a digit (0 through 9).
isgraph()
checks for any printable character except space.
islower()
checks for a lower-case character.
isprint()
checks for any printable character including space.
ispunct()
checks for any printable character which is not a space or an
alphanumeric character.
isspace()
checks for white-space characters. In the "C" and "POSIX"
locales, these are: space, form-feed ('/f'), newline ('/n'),
carriage return ('/r'), horizontal tab ('/t'), and vertical tab
('/v').
isupper()
checks for an uppercase letter.
isxdigit()
checks for a hexadecimal digits, that is, one of
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F.
2.关于size_type:
string类类型和许多其他库类型都定义了一些配套类型(companion type),通过这些配套类型,库类型的使用就能与机器无关(machine-independent)。
3.任何存储string的size操作结果的变量必须为string::size_type类型。特别重要的是:不要把size的返回值赋给一个int变量。
程序清单:
编译源程序:
g++ -o string_test string_test.cpp
执行程序:
./string_test
执行结果:
There are 4 characters in Hello,World!!!
hello,world!!!