C++ Tutorial/Data Types/char functions

Материал из C\C++ эксперт
Версия от 13:31, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

iscntrl

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::iscntrl; using std::isgraph; using std::isprint; using std::ispunct; using std::isspace; int main() {

  cout << "\nAccording to iscntrl:\nNewline " 
     << ( iscntrl( "\n" ) ? "is a" : "is not a" )
     << " control character\n"
     << ( iscntrl( "$" ) ? "$ is a" : "$ is not a" )
     << " control character\n";
  return 0;

}</source>

According to iscntrl:
Newline is a control character
$ is not a control character

isgraph

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::iscntrl; using std::isgraph; using std::isprint; using std::ispunct; using std::isspace; int main() {

  cout << "\nAccording to isgraph:\n"
     << ( isgraph( "Q" ) ? "Q is a" : "Q is not a" )
     << " printing character other than a space\nSpace " 
     << ( isgraph(" ") ? "is a" : "is not a" )
     << " printing character other than a space" << endl;
  return 0;

}</source>

According to isgraph:
Q is a printing character other than a space
Space is not a printing character other than a space

islower

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::islower; using std::isupper; using std::tolower; using std::toupper; int main() {

  cout << "According to islower:\n"
     << ( islower( "p" ) ? "p is a" : "p is not a" )    
     << " lowercase letter\n"
     << ( islower( "P" ) ? "P is a" : "P is not a" ) 
     << " lowercase letter\n"
     << ( islower( "5" ) ? "5 is a" : "5 is not a" ) 
     << " lowercase letter\n"
     << ( islower( "!" ) ? "! is a" : "! is not a" ) 
     << " lowercase letter\n";
  return 0;

}</source>

According to islower:
p is a lowercase letter
P is not a lowercase letter
5 is not a lowercase letter
! is not a lowercase letter

isprint

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::iscntrl; using std::isgraph; using std::isprint; using std::ispunct; using std::isspace; int main() {

  cout << "\nAccording to isprint:\n"
     << ( isprint( "$" ) ? "$ is a" : "$ is not a" )
     << " printing character\nAlert " 
     << ( isprint( "\a" ) ? "is a" : "is not a" )
     << " printing character\nSpace "
     << ( isprint( " " ) ? "is a" : "is not a" )
     << " printing character\n";
  return 0;

}</source>

According to isprint:
$ is a printing character
Alert is not a printing character
Space is a printing character

ispunct

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::iscntrl; using std::isgraph; using std::isprint; using std::ispunct; using std::isspace; int main() {

  cout << "\nAccording to ispunct:\n"
     << ( ispunct( ";" ) ? "; is a" : "; is not a" )
     << " punctuation character\n"
     << ( ispunct( "Y" ) ? "Y is a" : "Y is not a" )
     << " punctuation character\n"
     << ( ispunct("#") ? "# is a" : "# is not a" )
     << " punctuation character\n";
  return 0;

}</source>

According to ispunct:
; is a punctuation character
Y is not a punctuation character
# is a punctuation character

isspace

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::iscntrl; using std::isgraph; using std::isprint; using std::ispunct; using std::isspace; int main() {

  cout << "According to isspace:\nNewline " 
     << ( isspace( "\n" ) ? "is a" : "is not a" )
     << " whitespace character\nHorizontal tab " 
     << ( isspace( "\t" ) ? "is a" : "is not a" )
     << " whitespace character\n"
     << ( isspace( " is a" : " is not a" )
     << " whitespace character\n";
  return 0;

}</source>

According to isspace:
Newline is a whitespace character
Horizontal tab is a whitespace character
% is not a whitespace character

isupper

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::islower; using std::isupper; using std::tolower; using std::toupper; int main() {

  cout << "\nAccording to isupper:\n"
     << ( isupper( "D" ) ? "D is an" : "D is not an" ) 
     << " uppercase letter\n"
     << ( isupper( "d" ) ? "d is an" : "d is not an" ) 
     << " uppercase letter\n"
     << ( isupper( "8" ) ? "8 is an" : "8 is not an" ) 
     << " uppercase letter\n"
     << ( isupper("$") ? "$ is an" : "$ is not an" ) 
     << " uppercase letter\n";
  return 0;

}</source>

According to isupper:
D is an uppercase letter
d is not an uppercase letter
8 is not an uppercase letter
$ is not an uppercase letter

toupper and tolower

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::islower; using std::isupper; using std::tolower; using std::toupper; int main() {

  cout << "\nu converted to uppercase is " 
     << static_cast< char >( toupper( "u" ) )
     << "\n7 converted to uppercase is " 
     << static_cast< char >( toupper( "7" ) ) 
     << "\n$ converted to uppercase is " 
     << static_cast< char >( toupper( "$" ) ) 
     << "\nL converted to lowercase is " 
     << static_cast< char >( tolower( "L" ) ) << endl;
  return 0;

}</source>

u converted to uppercase is U
7 converted to uppercase is 7
$ converted to uppercase is $
L converted to lowercase is l

Using functions isalnum

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::isalnum; using std::isalpha; using std::isdigit; using std::isxdigit; int main() {

  cout << "\nAccording to isalnum:\n"
     << ( isalnum( "A" ) ? "A is a" : "A is not a" )
     << " digit or a letter\n"
     << ( isalnum( "8" ) ? "8 is a" : "8 is not a" )
     << " digit or a letter\n"
     << ( isalnum( "#" ) ? "# is a" : "# is not a" )
     << " digit or a letter\n";
  return 0;

}</source>

According to isalnum:
A is a digit or a letter
8 is a digit or a letter
# is not a digit or a letter

Using functions isalpha

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::isalnum; using std::isalpha; using std::isdigit; using std::isxdigit; int main() {

  cout << "\nAccording to isalpha:\n"
     << ( isalpha( "A" ) ? "A is a" : "A is not a" ) << " letter\n"
     << ( isalpha( "b" ) ? "b is a" : "b is not a" ) << " letter\n"
     << ( isalpha( "&" ) ? "& is a" : "& is not a" ) << " letter\n"
     << ( isalpha( "4" ) ? "4 is a" : "4 is not a" ) << " letter\n";
  return 0;

}</source>

According to isalpha:
A is a letter
b is a letter
& is not a letter
4 is not a letter

Using functions isdigit

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::isalnum; using std::isalpha; using std::isdigit; using std::isxdigit; int main() {

  cout << "According to isdigit:\n"
     << ( isdigit( "8" ) ? "8 is a" : "8 is not a" ) << " digit\n"
     << ( isdigit( "#" ) ? "# is a" : "# is not a" ) << " digit\n";
  return 0;

}</source>

According to isdigit:
8 is a digit
# is not a digit

Using functions isxdigit

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <cctype>

using std::isalnum; using std::isalpha; using std::isdigit; using std::isxdigit; int main() {

  cout << "\nAccording to isxdigit:\n"
     << ( isxdigit( "F" ) ? "F is a" : "F is not a" )
     << " hexadecimal digit\n"
     << ( isxdigit( "J" ) ? "J is a" : "J is not a" )
     << " hexadecimal digit\n"
     << ( isxdigit( "7" ) ? "7 is a" : "7 is not a" )
     << " hexadecimal digit\n"
     << ( isxdigit( "$" ) ? "$ is a" : "$ is not a" )
     << " hexadecimal digit\n"
     << ( isxdigit( "f" ) ? "f is a" : "f is not a" )
     << " hexadecimal digit" << endl;
  return 0;

}</source>

According to isxdigit:
F is a hexadecimal digit
J is not a hexadecimal digit
7 is a hexadecimal digit
$ is not a hexadecimal digit
f is a hexadecimal digit

Using strrchr: index a character in a string

<source lang="cpp">#include <iostream>

  1. include <string.h>

using namespace std; int main() {

  char *string1 = "this is a test";
  int c = "t";
  cout << "last occurrence of character "" << (char) c
       << "" is: \"" << strrchr( string1, c ) << "\"" << endl;
  return 0;

}</source>